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 ➜ General ➜ String matching in script

String matching in script

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


Posted by Fletchling   Australia  (54 posts)  Bio
Date Mon 11 Sep 2006 11:55 AM (UTC)
Message
Please point me at the help file, or somewhere in the forum to work out how to match strings within lua script. I'd like to do something like;

...stuff...
if %1 == (string.sub (line,1,((string.find (line, ",")-1)))) then
...other stuff...

"line" is a line of text read in via an io function
"%1" is text from an alias or trigger
The point of the whole line is to match part of a line of text up to the first comma against a variable set by an alias, then perform an action.

I can match numbers, but text matching in this form is eludling me.

Fletchling
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #1 on Mon 11 Sep 2006 11:04 PM (UTC)
Message
Hmmm, looks a very complicated line. You are trying to do two things here, so let's break them down and see what is happening:


  1. Find the word up to the comma
  2. See if it matches another word


It is unnecessary to use string.sub, because string.find will return a "capture", so this works:


line = "foo, bar"
c1, c2, s = string.find (line, "(.*),")
print (s)  --> foo


c1 and c2 are the starting and ending columns, but what you really want is the stuff before the comma, so putting (.*) means "capture this stuff".

If you are using MUSHclient 3.80, then string.match simplifies things because it doesn't return the columns:


line = "foo, bar"
s = string.match (line, "(.*),")
print (s)  --> foo


Now to see if it matches the target word. The way you have written it, it looks like this:


if %1 == s then
  -- do stuff
end -- if


The problem with this is that if %1 is "foo" then it will expand out to this:


if foo == s then
  -- do stuff
end -- if


This is wrong, you are testing against the variable "foo" not literally "foo".

You really want:


if "%1" == s then
  -- do stuff
end -- if


Put it all together and the final code looks like this:


if string.match (line, "(.*),") == "%1" then
  -- matched
end -- if


Or if you are using MUSHclient 3.79 or earlier:


c1, c2, s = string.find (line, "(.*),")
if s == "%1" then
  -- matched
end -- if



- Nick Gammon

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

Posted by Fletchling   Australia  (54 posts)  Bio
Date Reply #2 on Tue 12 Sep 2006 09:22 AM (UTC)
Message
Nick, thank you very much, quotes around the %1... I'd been looking at the wrong side of the == for ages.

Do you recommend upgrade to 3.80 under what circumstances (3.78 at the moment)?

Thanks again.
Fletchling the grateful
Top

Posted by Fletchling   Australia  (54 posts)  Bio
Date Reply #3 on Tue 12 Sep 2006 09:29 AM (UTC)
Message
and it works, woohoo.

I'll post the rest of the stuff in a little while, essentially I have a text file for each defined mud zone/area that contains;

ascci map,
notes,
speedwalks (inc interzone SWs),
mobs and equipment,
room pattern matching for onscreen larger maps with location identifier mark for the player (whereis.mud equivalent), and
basic 'take me there' SWs via hyperlink

thanks again
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #4 on Tue 12 Sep 2006 08:55 PM (UTC)
Message
Quote:

Do you recommend upgrade to 3.80 under what circumstances (3.78 at the moment)?


Well, I put a lot of dire warnings on the news page announcing its release, in case things went wrong with Lua 5.1.

So far, 30 copies have been downloaded (as at the moment I write this post), compared to 1,258 copies of version 3.78 (this month).

However there haven't been any complaints, except a problem with using the installer by someone who had changed directories around since installing an earlier copy (which would have happened with any new version install).

There have also only been 35 downloads of version 3.79 - I think most people download the copy mentioned on the Downloads page (for obvious reasons) rather than the most recently-announced one.

I don't see any real reason not to try it now - if you have lots of scripts and stuff like that, I suggest you back them up, just in case.

Actually, MUSHclient is a lot more popular than the number of registrations would indicate. It seems about 3,500 copies are downloaded each month, judging by the stats from the web server.

- Nick Gammon

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

Posted by Tsunami   USA  (204 posts)  Bio
Date Reply #5 on Tue 12 Sep 2006 09:22 PM (UTC)
Message
Been using 3.80 for a couple days now, and just upgraded my main plugin to use it. Everything seems to be working fine, and it was much easier that I thought it'd be. Only regrets is that now the old version isn't forwards compatible and the new version ent backwards compatible. Oh well, it'll teach the users to upgrade! -Tsunami
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.


19,645 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.