Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ String.match / String.find

String.match / String.find

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Scripts   (2 posts)  Bio
Date Sun 23 Sep 2007 12:20 AM (UTC)

Amended on Sun 23 Sep 2007 03:37 AM (UTC) by Scripts

Message
is there any way to dump the result of this directly into an array or a table?

i.e.

array={}
SomeTableElement={
"^(.+) searching for 2 (.+) matches here%.",
"^only searching for 1 (+.) here%."
}

for i=1,2 do
array = string.match(aString, SomeTableElement[i])
--do something with array here
end

I am trying to make a flexible string search that bases itself on valus in a table, but I can't seem to create a universal way to store all the data coming out of the string.match() function.

I'm extremely new to LUA and any help is greatly appreciated.

Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #1 on Sun 23 Sep 2007 04:56 AM (UTC)
Message
Yes you can, but I wonder what you are really trying to do here.

Since string.match can return multiple results, and you want to store them somewhere, you can make a table on-the-fly like this:


for i=1,2 do
  array = { string.match(aString, SomeTableElement[i]) }
  --do something with array here
end


Note the { ... } here which puts the string.match results into a table, which is then stored in "array".

The first thing I would do is change it from using "for i=1,2 do" to using ipairs, because I assume you will gradually add more items to the table, and don't want to have to keep remembering to change the 2 to 3, and then 4 and so on. This code will do the same thing, for any size table:


for k, v in ipairs (SomeTableElement) do
  array = { string.match(aString, v) }
  --do something with array here
end


I use the variables k and v to represent the Key and Value for the table. The key will simply be 1, 2, 3 ... and so on, and the value is the table item value for each key.

The other thing I worry about here is that you are applying the search to the same string, regardless of whether or not you get a match.

If the matches are mutually exclusive you might want to break out of the loop as soon as a match is made.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Scripts   (2 posts)  Bio
Date Reply #2 on Sun 23 Sep 2007 03:44 PM (UTC)

Amended on Sun 23 Sep 2007 03:48 PM (UTC) by Scripts

Message
Ahh, that's how you do it.

In my current code I do use ipairs(), and I have a test case for matching strings...just couldn't figure out how to put the data into a table. Eventually I want to use a lookup table to re-order the string.match() results for each case individually, but this gets me rolling again.

Thanks Nick, you're the man :)
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


13,801 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.