lua-users home
lua-l archive

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


> I've got bitten by a slight inconsistency when using table.concat()
> with start and end indexes.  When providing just a start index,
> table.concat() will happily return an empty string if that index is
> out of range, but when providing both the start and the end,
> table.concat() errors if the indexes are out of range.
> 
> This appears to be the same in 5.0 and 5.1 (so, consistent in that
> sense, I guess).
> 
> I am wondering if this is intentional (i.e., a documentation issue),
> or a bug?

It is intentional and according to the documentation. The point is that
there is no "out of range" concept when you provide the indices. If you
provide the indices, Lua assumes that this is what you want.

When you provide only the first index, the second defaults to the table
length.  This makes the second index smaller than the first, so the
interval is empty and the result is the empty string. See the manual:

  The default value for sep is the empty string, the default for i is 1,
  and the default for j is the length of the table. If i is greater than
  j, returns the empty string.

That is, Lua is not returning the empty string because the start index
is out of range, but because the given interval is empty.  If you
provide both indices with j < i, you get the same answer (independently
of whether i and j are "in" or "out" of range).

But when you provide both indices with j >= i, then you have a non-empty
range, so Lua will try to concatenate its elements. As they are not
strings, you get that error.

If you provide only the first index in a way that the interval is not
empty, you get the same error:

  > =table.concat(t, '', -3)
  stdin:1: bad argument #1 to 'concat' (table contains non-strings)

(Probably the error messsage should tell which table element it
is complaining about...)

-- Roberto