A thing to remember is that ipairs() will only iterate over a table whose children are numbered in order from 1 on. Any gaps and it will stop. So if you have a table with things at 1, 2, 3, 4, 5 it'll run 1 to 5 with no problem. But leave one out, 1, 2, 4, 5 and it will stop at 2 instead. pairs will instead run over all items in the table, even ones with no specified index and any index you choose. But it might be a TINY BIT slower, but usually not noticeable. Also, pairs will not start at 0 and will always start at 1. So if you're used to languages where arrays begin at 0, get used to using 1 instead.
for i, t in ipairs(table) do would be the equivalent to for i = 1, #table do where the # part returns the "length" of a tables numerically indexed children. (Also remembering how the numbers have to be in order.)
When removing a table child using table.remove, remember that if you are currently in a ipairs() loop, you will throw everything off so, as was mentioned elsewhere a few days ago, make sure to go backwards through the table. (for i = #table, 1, -1 do where -1 means to step backwards and it starts at the end of the table and goes towards the beginning.) table.remove will remove a child, and then reconfigure everything to keep them in order. For instance if you have 1, 2, 3, 4, 5 and you table.remove 2, it'll then move 3 to 2, 4 to 3 and 5 to 4. Also remember this will slow things down with larger tables since it is literally for looping through every table element to move them.
I hope that didn't confuse the crap out of you. That's all slightly more advanced. Start slow and play around a bit to get used to it. You'll see how awesome tables are.
Code: Select all
table = {
item = {
item = {
item = {
item = {
item = {
value = "Hahaha!!!"
}
}
}
}
}
}
print(table.item.item.item.item.item.value)
Edit: I edited it so hopefully it's correct now. If not, it's Nixola's fault.