[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: pairs(t, skey) and ipairs(t, skey)
- From: Andrew Starks <andrew.starks@...>
- Date: Wed, 2 Oct 2013 16:45:58 -0500
On Wed, Oct 2, 2013 at 4:29 PM, Sean Conner <sean@conman.org> wrote:
> Given:
>
> t_seq = {}
> for i = 1 , 99 do
> t_seq[i] = nil
> end
> t_seq[100] = "Boom"
>
> t_hole = {}
> t_hole[100] = "Boom"
>
> What should the following print?
>
> print(#t_seq)
> print(#t_hole)
Given that `nil` is used to denote a key that is "not there", the
above would not be a sequence.
Using Tim's example, I'd say that your two print statements should print "nil".
If I may channel Tim's intent for a moment:
EMPTY = {}
t_seq = {}
for i = 1, 99 do
t_seq[i] = EMPTY
end
t_seq[100] = "BOOM"
t_empty = {}
t_hash = { foo= "baz", bar = "dizzle"}
t_hybrid = {"first", "second", "third", hash = "value }
t_broken_sequence = {"first", "second", [4] = "fourth"}
print(#t_seq)
--> 100
print(#t_empty)
--> 0
print(#t_hash)
--> 0
print(#hybrid)
--> 3
print(#broken_sequence)
--> nil
Or something like it. Again, more of that "frowned upon" type of
proposal. It's not so important that "nil" be returned. It's more
important that the regular rules apply: nil is not a value that you
can effectively store in a table, apart from using it to know if that
index is non-existant.
-Andrew
- References:
- Re: pairs(t, skey) and ipairs(t, skey), Dirk Laurie
- Re: pairs(t, skey) and ipairs(t, skey), Paul K
- Re: pairs(t, skey) and ipairs(t, skey), Luiz Henrique de Figueiredo
- Re: pairs(t, skey) and ipairs(t, skey), Tim Hill
- Re: pairs(t, skey) and ipairs(t, skey), Luiz Henrique de Figueiredo
- Re: pairs(t, skey) and ipairs(t, skey), Tim Hill
- pairs(t, skey) and ipairs(t, skey), Andrew Starks
- Re: pairs(t, skey) and ipairs(t, skey), Luiz Henrique de Figueiredo
- Re: pairs(t, skey) and ipairs(t, skey), Tim Hill
- Re: pairs(t, skey) and ipairs(t, skey), Sean Conner