string.find

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

string.find

Maxime Chupin
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.
Thanks for your help.
Best regards,

--
Maxime Chupin
Reply | Threaded
Open this post in threaded view
|

Re: string.find

Patrick Mc(avery
Hi Maxime

I am just an amateur, most guys on this list are incredible programmers
but I think I know this one.

When Lua returns multiple results it's not in an array like PHP, perhaps.

Try:

a, b=string.find(a,"(%s+)");

HTH-Patrick

Reply | Threaded
Open this post in threaded view
|

Re: string.find

Patrick Mc(avery
In reply to this post by Maxime Chupin
sorry the use  of a, b might have been confusing in my last post as you
already used a in your code.

Try:
  x, y = some string.find code


Reply | Threaded
Open this post in threaded view
|

Re: string.find

Luiz Henrique de Figueiredo
In reply to this post by Maxime Chupin
> 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+)");

Try the code below.
        b,e=string.find(a,"(%s+)")

The b will contain 4 and e will contain 6.
b is the start of the first run of spaces in a, e is the end.
        "cou   cou cou";
         123456789
String positions in Lua start at 1.

Reply | Threaded
Open this post in threaded view
|

Re: string.find

Maxime Chupin
In reply to this post by Patrick Mc(avery


On 15 October 2010 21:17, Patrick Mc(avery <[hidden email]> wrote:
sorry the use  of a, b might have been confusing in my last post as you already used a in your code.

Try:
 x, y = some string.find code



  Ok! thanks. I used to code with perl, so it's difficult for me :-).

--
Maxime Chupin
http://mc.notezik.com
Reply | Threaded
Open this post in threaded view
|

Re: string.find

BogdanM
>>
>   Ok! thanks. I used to code with perl, so it's difficult for me :-).

This is exactly the kind of story that ends up breaking my heart every
single time. You have my full sympathy.

Best,
Bogdan

Reply | Threaded
Open this post in threaded view
|

Re: string.find

Shmuel Zeigerman
In reply to this post by Maxime Chupin
Maxime Chupin wrote:
> I tried :
> a="cou   cou cou";
> b=string.find(a,"(%s+)");
> but b[1] and b[2] does not work.

You can have such behavior by putting results into a table:
     b = { string.find(a,"(%s+)") }
Then:
b[1] will contain 4
b[2] will contain 6
b[3] will contain "   "

--
Shmuel

Reply | Threaded
Open this post in threaded view
|

Re: string.find

Norbert Kiesel
In reply to this post by Maxime Chupin
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