Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  This topic is locked: you cannot edit posts or make replies.     Home » Forums » CMUD General Discussion Goto page 1, 2  Next
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Thu Mar 27, 2008 12:20 am   

Storing a %1 variable
 
So, this is in relation to that thirst trigger...

When my character goes to sleep, I have it look for this pattern:

Code:
^You go to sleep in *$


The command text is:
Code:

posn=sleep
bed=%1
#say @bed


(the #say is merely for test purposes and will be removed)

Essentially, when I got to sleep in something, it looks like this:
Quote:
You go to sleep in a hammock.


I want to store that last word, hammock. But not just hammock, sometimes it's a cot, or a bed, etc... but right now bed isn't storing anything. :/
Reply with quote
Anaristos
Sorcerer


Joined: 17 Jul 2007
Posts: 821
Location: California

PostPosted: Thu Mar 27, 2008 12:23 am   
 
You have to indicate the desire to capture the data in the trigger by surrounding it with parentheses. So:
Code:

^You go to sleep in a (*)$
_________________
Sic itur ad astra.

Last edited by Anaristos on Thu Mar 27, 2008 12:24 am; edited 1 time in total
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Thu Mar 27, 2008 12:24 am   
 
Alright, so the ( ) mean you want to store it, I thought it was the other way around. Thanks.
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Thu Mar 27, 2008 12:25 am   
 
Also, I need to differentiate between a and the

so

Code:
^You go to sleep in [a|the] (*)$

?

And actually, since I can also just:

Quote:
You go to sleep.


I should find a way to include the "in" as a part of the variable, it's not always there.

That away, if I just go to sleep, bed=NULL
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Thu Mar 27, 2008 12:47 am   
 
if you want to use a stringlist pattern, the correct braces are {}. [] is used for the range pattern, which is any combination of the contained characters

[a|the] -- will match on any combination of a, |, t, h, and e.
{a|the} -- will only match on the word "a" or the word "the".
_________________
EDIT: I didn't like my old signature
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5187

PostPosted: Thu Mar 27, 2008 12:49 am   
 
Try enter this into the command line and deleting any other triggers you have that are related to this.
Code:
#TRIGGER {^You go to sleep(*).$} {
 posn="sleep"
 bed=%word(%1,%numwords(%1)
}
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Thu Mar 27, 2008 12:58 am   
 
Vijilante wrote:
Try enter this into the command line and deleting any other triggers you have that are related to this.
Code:
#TRIGGER {^You go to sleep(*).$} {
 posn="sleep"
 bed=%word(%1,%numwords(%1)
}


How does bed avoid capturing any of the following:

You go to sleep on a soft bench.

You go to sleep on a soft, white cloud.

You go to sleep on a black silk-encased mattress.

You go to sleep on a pile of pillows and blankets.


I just changed my triggers, and it looks like it should work in all these situations. But if yours is more efficient, then I'll be happy to switch. Here's mine:

Code:

^You go to sleep * (*).$

posn=sleep
bed=%1


and

Code:

^You go to sleep.$

posn=sleep
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5187

PostPosted: Thu Mar 27, 2008 1:10 am   
 
Oops I was missing a closing parenthesis there.
Code:
#TRIGGER {^You go to sleep(*).$} {
 posn="sleep"
 bed=%word(%1,%numwords(%1))
}
In terms of effeciency I don't think there is any real difference between using 2 triggers or 1 with the additional parsing. As to how it works look up the %word and %numwords functions in the help.
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Thu Mar 27, 2008 1:12 am   
 
Alright. Thanks, at the very least I can learn from it. :)
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
JQuilici
Adept


Joined: 21 Sep 2005
Posts: 250
Location: Austin, TX

PostPosted: Thu Mar 27, 2008 5:14 am   
 
Or even
Code:
#REGEX {^You go to sleep(?: .* (\w+)|)\.$} {
  posn="sleep"
  bed=%1
}

One trigger, no branches - the regex engine figures out which case applies. And it sets the bed variable to empty when there's no bed.
_________________
Come visit Mozart Mud...and tell an imm that Aerith sent you!
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Thu Mar 27, 2008 9:10 am   
 
Alright, so this works because...

hmmm .* makes sense to me,

(\w+)|) does not make sense to me

nor does ?:

It did work though.
Reply with quote
Guinn
Wizard


Joined: 03 Mar 2001
Posts: 1127
Location: London

PostPosted: Thu Mar 27, 2008 11:21 am   
 
If you didn't have the ?: then it would store ' .* (\w+)|' as %1. Using ?: before means don't remember this bit for purposes of a %1 match.
The (\w+)| bit is saying either match a word or match nothing. You could also replace the | with ?, which means match 1 or 0 occurences of the pattern and might be a more standard way of doing it.

Code:
#REGEX {^You go to sleep(?: .* (\w+)?)\.$} {
  posn="sleep"
  bed=%1
}


Edit: Scratch that last bit, if you replace | with ? then it doesn't clear the 'bed' variable. I should know better than to correct JQuilici Laughing
_________________
CMUD Pro, Windows Vista x64
Core2 Q6600, 4GB RAM, GeForce 8800GT
Because you need it for text... ;)
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Thu Mar 27, 2008 11:29 am   
 
?: Don't remember what bit? if the .*(/w+) is storing a word, but ignoring none words (in case none exist) then what am I not remembering? the "in a|the" ? How does it differentiate, or is that what the : does?

Either way, I can't test anything yet. I can't get my CMUD functional...
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Thu Mar 27, 2008 11:55 am   
 
Guinn wrote:
You could also replace the | with ?, which means match 1 or 0 occurences of the pattern and might be a more standard way of doing it.

Code:
#REGEX {^You go to sleep(?: .* (\w+)?)\.$} {
  posn="sleep"
  bed=%1
}


Edit: Scratch that last bit, if you replace | with ? then it doesn't clear the 'bed' variable.


Your pattern's incorrect - that'll match 0 or 1 words, but will always try to match the spaces and .*. You mean ^You go to sleep(?: .* (\w+))?\.$ which will match 0 or 1 of the (?: .* (\w+)) part.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Thu Mar 27, 2008 12:11 pm   
 
Isn't that what I have?
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
JQuilici
Adept


Joined: 21 Sep 2005
Posts: 250
Location: Austin, TX

PostPosted: Thu Mar 27, 2008 12:20 pm   
 
I wonder why I didn't do it Fang's way? Perhaps it was late...whatever. I guess there's more than one way to skin a cat, even in regexes. Smile

And to answer chamenas' earlier question, the pattern (?:regex) works just like (regex) in that it groups things in the pattern, however, it doesn't create a backreference. IOW, it doesn't 'remember' anything into %1...as far as the numbered patterns go, that set of parens doesn't exist. If that part of the pattern were '( .* (\w+))?' or '( .* (\w+)|)', you'd have to assign bed = %2, because %1 would have everything after the word sleep, and not just the last word.

So, to break it down further, my original version had '(?:regex1|regex2), which is an alternation - it matches either regex1 or regex2, and doesn't bother putting the results of that match into a numbered backreference. In this example, regex1 was ' .* (\w+)', which matches a bunch of words, like 'in a comfy bed', and catches just the last one into %1. Regex2 was completely blank, so it matches a zero-length string (and thus made the whole thing match 'You go to sleep.').

You can find out about all sorts of wonderful regex syntax here, and it even has a good tutorial. CMUD uses the PCRE library, and Vijilante has updated it to the latest version in CMUD 2.20.
_________________
Come visit Mozart Mud...and tell an imm that Aerith sent you!

Last edited by JQuilici on Thu Mar 27, 2008 12:38 pm; edited 1 time in total
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Thu Mar 27, 2008 12:35 pm   
 
Yeah, I was already given the link. But I think reading it, trying and asking questions here is the best way to learn.

So ?: says, "don't remeber" \w means "grab last word"?
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
JQuilici
Adept


Joined: 21 Sep 2005
Posts: 250
Location: Austin, TX

PostPosted: Thu Mar 27, 2008 12:41 pm   
 
No...\w means 'a word character'. It's equivalent to [a-zA-Z0-9_], typically. So, (\w+) means, 'a series of one-or-more word characters (and stick it in a backreference)'. Since that part of the pattern is bounded by a space (before) and a literal . (after), it should match the final word in the sentence.
_________________
Come visit Mozart Mud...and tell an imm that Aerith sent you!
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Thu Mar 27, 2008 12:54 pm   
 
(?: .* (\w+)?)\.$

so ?: means: don't remember this

.* since it's in parentheses means: put this in %1

But I don't get how it isolates the last character,

(\w+) is what forces it to store a word then?

and ?) I assume means: don't remember stuff after this.
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Thu Mar 27, 2008 1:06 pm   
 
As I said in my post, that last question mark is in the wrong place. It needs to be after the last bracket, not before it.

Brackets () normally mean "capture this". Brackets (?: ) mean "don't capture this". The reason we use the brackets that aren't captured is because modifiers like +, ? and * apply to whatever came before them - if what came before them is brackets, the modifier applies to the brackets as a whole. Sometimes we want to use that without capturing anything.

So. The (?: ) brackets (the closing bracket is the final one, the one before the ?\.$ ) mean "don't capture these brackets". The .* means "any number of any character". The next set of brackets mean "capture this". What's being captured is \w+ - which is a word character, a letter or a number. The plus means "one or more of what came before", so "one or more word characters". Then you have the two closing brackets. The ? means "0 or 1 of what came before" - the (?: ) brackets came before it, so it makes everything inside those brackets optional. Then you have a . (escaped so that it doesn't mean "any character") and $, which means the end of the line.

Seriously, check out the regex website we keep linking.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Thu Mar 27, 2008 1:09 pm   
 
Every bracket on the tutorial site is a ( ) in CMUD?
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Thu Mar 27, 2008 1:14 pm   
 
Not sure what you're asking. Regexes in CMUD work exactly the same as regexes on that site.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Thu Mar 27, 2008 1:24 pm   
 
Every time I use a bracket, someone says I should use parentheses.

Say I want to say it could be a or the, on the site I'm fairly certain it says [a|the]
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Thu Mar 27, 2008 1:33 pm   
 
Parenthesis and brackets are the same thing. Brackets or parenthesis (), square brackets [], triangular brackets <>, braces or curly brackets {}
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Thu Mar 27, 2008 1:47 pm   
 
Fang Xianfu wrote:
Parenthesis and brackets are the same thing. Brackets or parenthesis (), square brackets [], triangular brackets <>, braces or curly brackets {}


Thanks. I am learning a lot, even if I ask an endless flow of questions.
Reply with quote
Display posts from previous:   
Post new topic   This topic is locked: you cannot edit posts or make replies.     Home » Forums » CMUD General Discussion All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net