|
Shezmu Wanderer
Joined: 23 Apr 2010 Posts: 53
|
Posted: Fri Apr 23, 2010 10:25 pm
Counting occurences in string |
Okay, some of the more advanced CMud users will laugh at me for this one. I'm trying to count the number of occurences of the word "and" in the following string:
You join Steve and Mary and Mark and Frank and John in a threnody ritual.
What I want to build is a counter that shows me how many people are already in the ritual (to which I will increment when additional people join in until we reach enough people to complete the ritual.)
Like I said, this should be an easy one to solve, but I'm wracking my brain trying to find it. Suggestions? |
|
|
|
DraxDrax Apprentice
Joined: 22 Mar 2009 Posts: 149
|
Posted: Fri Apr 23, 2010 11:17 pm |
%eval(%numwords("<string>"," and ")-1) should give you the number of times 'and' occurs in the string, or 0 if it doesn't appear.
#SAY %eval(%numwords("You join Steve and Mary and Mark and Frank and John in a threnody ritual."," and ")-1)
returns 4.
Be sure to have spaces around the 'and' or the count will include other words which contain 'and', like someone named Moander, for instance. |
|
|
|
Shezmu Wanderer
Joined: 23 Apr 2010 Posts: 53
|
Posted: Sat Apr 24, 2010 2:36 am |
Worked perfectly, Drax. Thanks for the help. I knew someone could answer this one easily. I'm still learning the ropes with CMud. Just switched over after Portal went under. I promise, I'll be back with more questions ;)
|
|
|
|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: Sat Apr 24, 2010 1:26 pm |
You don't actually need %eval there. Just use parentheses:
(%numwords("You join Steve and Mary and Mark and Frank and John in a threnody ritual."," and ")-1) |
|
|
|
|
|