Note both tables SHOULD be identical for all purposes but those of foreach().
{0,1,2} is different to {[0]=0,[1]=1,[2]=2}
{0,1,2} is actually {[1]=0,[2]=1,[3]=2}
remember that lua indexes tables from 1 by default.
foreach doesn't know about the 0th index so it doesn't do anything with it.
Indeed, it appears like all table-related functions are completely unaware of possibility of non-sequential indexing, to the point where it would appear like it would be seem like a good idea to keep a separate table (collection) for indexes.
Also, it's okay to do c={[0]=0,1,2} to initialize the table in the second case.
that said i really miss "pairs()" and being able to iterate through the non sequential keys.
foreach() and all() really need to behave just like pairs(). The current implementation only does the 1..n array part for dubious reasons, and I haven't seen any carts in the wild taking advantage of the fact that you can 'hide' non-iterated values inside a table because of this. So, unless I discover a good reason to keep the current behaviour, I'll look at iterating over all elements like pairs() in 0.1.2 (or maybe 0.2 if it turns out to be tricky)
pls pls pls allow us to get the keys with pairs() too. that would be awesome.
The other reason to keep foreach is that pairs() doesn't guarantee ordering of tables. unless in your implementation you do something like returning the sorted int keys first, then return the other types of keys. dunno tho.
yeah, i think leave all() as it is, it works fine for sequences, and changing it will likely break things.
add in pairs() just like the standard lua one for use in hashmaps would be ideal.
another thing - del() doesn't renumber the remaining members of the collection, so you get a weird collection where the first member is nil but the others exist somehow.
yeah del() is odd without renumbering, something like table.remove would be more useful in most cases i think, although del removed by value rather than by key.
I can hammer nails with a wrench, too!
That came out a bit strong. Sorry.
[Please log in to post a comment]