[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: ipairs and extending numeric for loop
- From: "Juri Munkki" <jm_list@...>
- Date: Tue, 25 May 2010 11:59:10 +0300
On Tue, May 25, 2010 2:05 am, Alexander Gladysh wrote:
> Funny thing that I was sure that it is undefined behavior, just like
> with next/pairs.
Yet the reference manual doesn't say this. In fact, modifying my previous
example:
print("With ipairs");
a = { 1, 2, 3 };
for i,v in ipairs(a) do
a[2] = nil;
print(i,v)
end
print("Without ipairs");
b = { 1, 2, 3 };
for i = 1, #b do
b[2] = nil;
print(i, b[i]);
end
This will print:
With ipairs
1 1
Without ipairs
1 1
2 nil
3 3
Replacing ipairs with pairs will result in both indexes 1 and 3 being
printed out.
Any comments on the "for i,v in 1, mytable do" and "for i,v in mytable do"
ideas? Having had time to think about them since yesterday afternoon, it
still feels like the syntax could be both a performance win and easily
understood syntax as well. Plus, if ipairs is deprecated and removed,
there would be a clean, safe and easy replacement for it.
Juri Munkki
jmunkki@portalify.com