[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: (newbie) Lua equivalent of split() in perl?
- From: Shmuel Zeigerman <shmuz@...>
- Date: Sat, 08 Oct 2005 09:01:51 +0200
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
name, birthyear, sex = split(input_text)