This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ChiknNuggets's profile picture

ipairs is skipping...

Started by ChiknNuggets, 03 January 2013 - 04:43 AM
ChiknNuggets #1
Posted 03 January 2013 - 05:43 AM
for i,v in ipairs(filelist) do
			    if string.lower(string.sub(v,#v-3,#v)) == ".ico" then
							    table.remove(filelist,i)
			    end
end

so got this code, and well it seems that because of the fact i remove the input(i) from the table if it ends in ".ico" i think it messes up the order that it goes in, for example say i=5 and v does end in “.ico” it deletes it, but now 6 has become 5, meaning that it actually skips that particular number, any solutions to this?

Much Appreciated
GopherAtl #2
Posted 03 January 2013 - 06:11 AM
this was covered pretty extensively a few days back in this thread, there are multiple possible solutions listed there.
Orwell #3
Posted 03 January 2013 - 06:13 AM
Quick and dirty solution is to use 'pairs' because it uses a different way of indexing that can handle this. :)/> (each key has a pointer to the next key)

Edit: GopherAtl's answer is more thorough. :)/>
ChunLing #4
Posted 04 January 2013 - 08:14 AM
For removing entries from an iterative table, I prefer to decrement a numeric for loop. But using pairs would be safer.