How do I get a certain number of characters in a variable?

Post Reply
CRCfail
Posts: 4
Joined: Thu Jun 05, 2014 6:36 am

How do I get a certain number of characters in a variable?

Post by CRCfail »

For example, if there is one troll:

Window:
There is a troll here.

Trigger:
There is a (\w+) here.
Action:
send("attack " .. matches[2])

Works just fine to kill one troll. But where there are two, three, four, etc:

Window:
There are two trolls here.
Trigger:
There are two (\w+) here.
Action:
send("attack " .. matches[2])

Will send "attack trolls" with the 's' on the end and the game responds with "You don't see 'trolls' here".

How do I make it only grab the first few characters like tro, trol, or troll, instead of the entire word?

Thanks for any help

User avatar
demonnic
Posts: 886
Joined: Sat Dec 05, 2009 3:19 pm

Re: How do I get a certain number of characters in a variabl

Post by demonnic »

You might consider shifting the regex up a bit.

There are (\w+) (\w+?)s? here\.

Should match whether there are two or fifteen... won't match on fifty two or fifty-two. Don't know if you need it to be that flexible or not. You could also set up a table to translate "two" into 2 an so forth. The second part just strips the s if it's pluralized.

To answer the original question though:
Code: [show] | [select all] lua
howManyCharactersToTrim=1 --or whatever you're trying to trim off the end.
-- this bit of complex voodoo because you're going to pass the positionToTrimAt into string.sub. -1 is the last
-- character in the string, so the next-to-last is -2, and so on. The string.sub will essentially be saying
-- take everything from the 1st character to the <nth>-to-last character. Check out 
-- http://lua-users.org/wiki/StringLibraryTutorial on the last page for a good explanation and set of examples
positionToTrimAt=(howManyCharactersToTrim * -1) - 1 
target = string.sub(matches[2], 1,positionToTrimAt) 
send("attack " .. target)

CRCfail
Posts: 4
Joined: Thu Jun 05, 2014 6:36 am

Re: How do I get a certain number of characters in a variabl

Post by CRCfail »

Okay the (w+)s works to grab "troll" out of "trolls" but when a phrase like "two stone giants" comes up, it won't grab "stone" because there's no S.

I'm having a difficult time understanding lua characters of ^ , ? , * , \ , etc.

I can't seem to find these in the manual either.

CRCfail
Posts: 4
Joined: Thu Jun 05, 2014 6:36 am

Re: How do I get a certain number of characters in a variabl

Post by CRCfail »

I fixed it by having both these lines:

There (is|are) (a|an|two|three|four|five|six|seven|eight) (\w+)
There (is|are) (a|an|two|three|four|five|six|seven|eight) (\w+)s

So now it trigger on "There are two orcs" for attack orc, and "There are two stone giants" for attack stone.

Thanks for your help!

User avatar
SlySven
Posts: 1023
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK
Discord: SlySven#2703

Re: How do I get a certain number of characters in a variabl

Post by SlySven »

Don't forget that mayhaps you might also need to consider other plural forms, for instance "es"! The only examples I can think of off the top of my head are "finches" and "wenches" - and, whilst I doubt there are too many MUDs with small seed eating killer birds, there could be one or two that have humanoid females that can get aggressive under some circumstances... :)

CRCfail
Posts: 4
Joined: Thu Jun 05, 2014 6:36 am

Re: How do I get a certain number of characters in a variabl

Post by CRCfail »

I have run across "Cyclopes", which is suprisingly the correct plural form.

Jor'Mox
Posts: 1146
Joined: Wed Apr 03, 2013 2:19 am

Re: How do I get a certain number of characters in a variabl

Post by Jor'Mox »

You also have completely irregular plurals, like children, mice, and deer.

User avatar
demonnic
Posts: 886
Joined: Sat Dec 05, 2009 3:19 pm

Re: How do I get a certain number of characters in a variabl

Post by demonnic »

So yeah... some of them you won't catch with one or two very simple triggers.

User avatar
SlySven
Posts: 1023
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK
Discord: SlySven#2703

Re: How do I get a certain number of characters in a variabl

Post by SlySven »

And that is what makes script (and code) writing, um, interesting as in the manner of the Chinese curse. :P

User avatar
Akaya
Posts: 414
Joined: Thu Apr 19, 2012 1:36 am

Re: How do I get a certain number of characters in a variabl

Post by Akaya »

Don't forget the meece... mooses... moose. W/e

Post Reply