lua-users home
lua-l archive

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



On Jul 6, 2013, at 12:05 PM, Jorge <xxopxe@gmail.com> wrote:

Even with that explicit length, you can not have a nil inside a string. That would not make any sense. After all, a string is a array of char! :)

What I would find silly is the following:

 --compute the sum of an array:
local sum=0
for i=1, #a do
    if a[i] then sum=sum+a[i]
end

An array is a very regular structure. This allows for compact agorithms. Having holes in it defeats this. If it has holes, it is not an array, it's a dictionary, and you use other algorithms. 

I agree, that would be silly if the domain of the array were numbers, since you would know the intended domain and decree that all it contained were numbers. Of course, if some entires were string etc, then you WOULD need something like the above code, so I'm not sure how nil is the issue here.

The comparison was merely between the crisply defined behavior of strings versus the weaker array.

--Tim