lua-users home
lua-l archive

Re: pairs(t, skey) and ipairs(t, skey)

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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

AltStyle によって変換されたページ (->オリジナル) /