Log In  

Note both tables SHOULD be identical for all purposes but those of foreach().

P#12256 2015-08-04 08:59 ( Edited 2015-08-07 09:18)

{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.

P#12259 2015-08-04 11:02 ( Edited 2015-08-04 15:02)

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.

P#12262 2015-08-04 11:21 ( Edited 2015-08-04 15:22)

Aheh, whoops.

P#12263 2015-08-04 11:50 ( Edited 2015-08-04 15:50)

that said i really miss "pairs()" and being able to iterate through the non sequential keys.

P#12266 2015-08-04 12:12 ( Edited 2015-08-04 16:12)

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)

P#12280 2015-08-04 15:15 ( Edited 2015-08-04 19:15)

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.

P#12281 2015-08-04 15:27 ( Edited 2015-08-04 19:27)

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.

P#12300 2015-08-05 06:48 ( Edited 2015-08-05 10:48)

forpairs()!

P#12301 2015-08-05 07:42 ( Edited 2015-08-05 11:42)
  • on this.

I actually assumed it worked like pairs() anyway.

P#12302 2015-08-05 10:39 ( Edited 2015-08-05 14:39)

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.

P#12381 2015-08-06 18:11 ( Edited 2015-08-06 22:11)

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.

P#12386 2015-08-06 21:50 ( Edited 2015-08-07 01:50)

you could code it yourself but it's moar tokens.

P#12389 2015-08-06 23:00 ( Edited 2015-08-07 03:00)

I can hammer nails with a wrench, too!

That came out a bit strong. Sorry.

P#12400 2015-08-07 05:18 ( Edited 2015-08-07 09:19)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 05:19:20 | 0.010s | Q:26