lua-users home
lua-l archive

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


May be is the reason you had mentioned.
Let's take a look at the examples below:
---------------------
array = {1,2,3}
--Example A:
while true do
  if table.remove(array,1) == nil then
  break;
  end;
  ...
end;

--Example B:
while true do
  if array[1] == nil then
  break;
  end;
  table.remove(array,1);
  ...
end;
---------------------
In some situation, it can simplified the code while "table.remove" accept the index len+1.
But i think example B is more clearly to tell what does this codes do, and can adapt to future changes of lua(maybe table.remove(len+1) will trigger an error one day.)

thx for the reply.

On 2018-06-14 04:31 AM, siney wrote:
>
> I think it just tell you no element been removed , you should stop to
> call remove again by in loop,
>
> You can check returned value is nil to know index is invalid without
> triggering an error.
>