lua-users home
lua-l archive

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


On Fri, 2010-10-15 at 21:18 +0100, Maxime Chupin wrote:
> Hi,
> 
> I am learning lua because I want to use LuaTeX. My question is
> probabely very simple but...
> I want to use string.find and it returns two numbers, but I do not
> know how to use this return. 
> I tried :
> a="cou   cou cou";
> b=string.find(a,"(%s+)");
> but b[1] and b[2] does not work.

Aside from the generic advise to read up on string.find in [1], for your
concrete question: find returns multiple values (start, end, and
captures if they exist), but Lua drops all the ones you don't assign.
So

s, e, c1 = string.find(a, "(%s+)")

would do the trick.  Alternatively, you can generate an array containing
all the return values and assign that

b = { string.find(a, "(%s+)") }
and then use b[1], b[2], b[3]

BTW: you can also use a:find("(%s+)") and in case you just want the
captured substring w/o the start/end indices you can use
b = a:match("(%s+)")

</nk>

[1] http://www.lua.org/manual/5.1/manual.html#pdf-string.find