lua-users home
lua-l archive

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


Gregory Benjamin wrote:
How does one do the equivalent of
the following Perl code:

($name,$birthyear,$sex) = split();

when presented with a line of input text like:

"jane 2000 f"

which results in $name = "jane", $birthyear = 2000, $sex = "f"

in Lua 5.0?

There is no built-in split in Lua 5.0.
But it's easy to define one:

function split(text, start)
  local s,e,word = string.find(text, "(%S+)", start or 1)
  if s then return word, split(text, e+1); end
end

name, birthyear, sex = split(input_text)

--
Shmuel