Incrementing table index

I was doing a test program where I use a button to change the index of a table, then display text from the table in a text field.

[lua]local function handleButtonEvent( event )

if ( “ended” == event.phase ) then
if arrayindex==4 then
arrayindex=0
end

arrayindex=arrayindex+1
textbox.text = testarray[arrayindex]
print( arrayindex )
end
end[/lua]

What I want it to do is reset the table index to 0 when it hits 4, then immediately increment back to 1 and print the first item in the table. (I have 3 items in the table). According to the terminal, when the button push increases the arrayindex variable to 4, it is not reset to 0 immediately, but print (arrayindex) indicates the value makes it to the bottom of the if statement as 4.

At the next button push, it resets to 1 as originally intended.

I’m not sure why the value stays as 4 until the end of the if statement.

just use

arrayIndex =((arrayIndex+1)%3)+1

this will auto cycle arrayIndex 1,2,3,1,2,3,…
no need for the if to check for arrayIndex == 4

It actually increments 3,2,1, but otherwise works as intended. 

Thanks.

just use

arrayIndex =((arrayIndex+1)%3)+1

this will auto cycle arrayIndex 1,2,3,1,2,3,…
no need for the if to check for arrayIndex == 4

It actually increments 3,2,1, but otherwise works as intended. 

Thanks.