Next: 8.5 Persistence Up: 8 Some Examples Previous: 8.3 Manipulating Strings

8.4 Variable number of arguments

Lua does not provide any explicit mechanism to deal with variable number of arguments. However, one can use table constructors to simulate this mechanism. As an example, suppose a function to concatenate all its arguments. It could be written like

function concat (o)
  local i = 1
  local s = ''
  while o[i] do
    s = s .. o[i]
    i = i+1
  end
  return s
end
To call it, one uses a table constructor to join all arguments:
  x = concat{"hello ", "john", " and ", "mary"}


Next: 8.5 Persistence Up: 8 Some Examples Previous: 8.3 Manipulating Strings