lua-users home
lua-l archive

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



On 7-Oct-05, at 11:50 PM, 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?

Simple solution:

local _, _, name, birthyear, sex = string.find(line, "(%S+)%s+(%S+)%s+(.*)")

More examples on the wiki: http://lua-users.org/wiki/StringRecipes