[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Unexpected table.remove behaviour.
- From: lostgallifreyan <lostgallifreyan@...>
- Date: Tue, 23 Jun 2009 23:03:50 +0100
No, not that one.. :) I read about the fixed problem with out of range indices, this is maybe a different thing.
If you set an element in an empty table as follows, you can't remove it with table.remove!
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
Is this intentional? I notice that with Lua v4, you can do it. x will be nil. Here are two different scripts for same purpose of syncing two arrays according to operations carried out on one of them:
for N=#X,1,-1 do
if X[N]==X[N+1] then table.remove(X,N+1) table.remove(Y,N+1) end
for N=getn(X),1,-1 do
if X[N]==X[N+1] then tremove(X,N+1) tremove(Y,N+1) end
for N=1,getn(X) do print(X[N]) end
for N=1,getn(Y) do print(Y[N]) end
The second one (run on Lua v4) behaves as expected, and desired, the element count is equal in each array and each element is correctly paired across tables as it should be. What do I have to do to get the same behaviour on v5? Note that the manual for v5 says directly that table.remove will remove the element, so this finding is in direct contradiction to the manual, so the point surely needs answering even if it's not new..
PS. I notices 'setn' is deprecated. If I wanted to do it, can it be done some other way, and if so, how?