table.insert issue

I’m using the following to insert an image into the 1st index of a table

table.insert(t, 1, image)

From what I read it’s supposed to enter that image into table t at the 1st position shifting all the other images in that table down…my table I has 3 images [1], [2] and [3] so when I insert the new image using the code above I should then have [1], [2], [3] and [4] but sometimes (doesn’t do it all the time) the result is [1], [2], [3] and [5]

This causes and error in my code as I’m looping though my table like so

for i=1,#t do end

I’m wondering why it skips an index randomly and what can I do to prevent this?

I went about it a different way and got it to work. I’m still curious as to what the issue was / is with the method above though.

i’d maybe start looking for nils sneaking in. they can make the numeric indices “sparse” (or cause your expected count to off-by-one if at end, ie t={1,2,3,nil} ==> #t==3) or removals via t[n]=nil. print before/after, wait for problem, inspect for nils. maybe??

I went about it a different way and got it to work. I’m still curious as to what the issue was / is with the method above though.

i’d maybe start looking for nils sneaking in. they can make the numeric indices “sparse” (or cause your expected count to off-by-one if at end, ie t={1,2,3,nil} ==> #t==3) or removals via t[n]=nil. print before/after, wait for problem, inspect for nils. maybe??