On 06/07/13 13:05, Tim Hill wrote:
Exactly!!
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.
It does seem awfully
strange to me: Lua's strings can contain any byte, because
they store the length separately from the string data.
This is generally considered a good thing, especially
compared to C strings - it takes length lookup from O(n)
to O(1) and allows them to contain arbitrary binary data.
Yet Lua's arrays don't have this same feature, and instead
use the C-string method of a magic terminating value that
can't appear anywhere in the array, which limits what they
can store. (Though I assume length lookup is still O(1) in
this case!) And as a "bonus" it makes the # operator's
behaviour somewhat strange as well - not always very
reliable or useful, and apparently rather confusing to
some people.
|