lua-users home
lua-l archive

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


2010/7/13 Roberto Ierusalimschy <roberto@inf.puc-rio.br>:
>> I stumbled accross something I couldn't find an anwer to in the Lua
>> manual. How is the bahavior of multiple assignments to the same
>> variable defined, e.g.
>>
>> a,a = 1,2
>>
>> or
>>
>> t[1],t[1] = 1,2
>>
>> ?
>
> It is not specified. Why do you want to know?
>
> -- Roberto
>

pseudoexample (I solved the problem in a different way, but my
original question remained, so I was looking for an answer) - the code
moves non-nil values to the front of the array where they stack up.
The sample is simplified - using table.remove would work in this case
but it would break my original code as I use the array in a non linear
way:

local pos = 1
for i=1,#arr do
  if arr[i] ~= nil then
    arr[pos],arr[i],pos = arr[i],nil,pos+1
  end
end

I realized that until the first nil value is encountered, that it
would assign the value of the element and nil to the same index -
which is undefined as I know now. Thanks for that hint... maybe the
manual on assignments should cover this case and state that the
behavior is undefined?

Eike