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

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
Llohr
Apprentice


Joined: 17 May 2005
Posts: 108

PostPosted: Wed Jun 06, 2012 9:53 pm   

Quick trigger question
 
So, on a whim and to no real purpose, I decided to make a trigger to record the results of a randomly generated lottery.

The mud periodically sends:

Winning Lotto Numbers are: 36 79 34 33 03 94 05 89 49 06

And I attempted to create a trigger that would create a variable for each number not seen before, and add 1 to the corresponding variable if it already exists. Here's the XML:

<trigger priority="4420" id="442">
<pattern>Winning Lotto Numbers are~: (%n) (%n) (%n) (%n) (%n) (%n) (%n) (%n) (%n) (%n)</pattern>
<value>#if (@%1) {#var %1 (@%1 + 1) {0} {lotto}} {#var %1 {1} {0} {lotto}}
#if (@%2) {#var %2 (@%2 + 1) {0} {lotto}} {#var %2 {1} {0} {lotto}}
#if (@%3) {#var %3 (@%3 + 1) {0} {lotto}} {#var %3 {1} {0} {lotto}}
#if (@%4) {#var %4 (@%4 + 1) {0} {lotto}} {#var %4 {1} {0} {lotto}}
#if (@%5) {#var %5 (@%5 + 1) {0} {lotto}} {#var %5 {1} {0} {lotto}}
#if (@%6) {#var %6 (@%6 + 1) {0} {lotto}} {#var %6 {1} {0} {lotto}}
#if (@%7) {#var %7 (@%7 + 1) {0} {lotto}} {#var %7 {1} {0} {lotto}}
#if (@%8) {#var %8 (@%8 + 1) {0} {lotto}} {#var %8 {1} {0} {lotto}}
#if (@%9) {#var %9 (@%9 + 1) {0} {lotto}} {#var %9 {1} {0} {lotto}}
#if (@%10) {#var %10 (@%10 + 1) {0} {lotto}} {#var %10 {1} {0} {lotto}}</value>
</trigger>

The trigger fires at every lotto, but creates no variables, nor will it add 1 to a manually created variable.

Would anyone like to tell me what foolish mistake I have made?
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Wed Jun 06, 2012 10:34 pm   
 
You need to use @{X} to make indirect references to variables.
_________________
EDIT: I didn't like my old signature
Reply with quote
Llohr
Apprentice


Joined: 17 May 2005
Posts: 108

PostPosted: Thu Jun 07, 2012 12:41 am   
 
I knew I'd seen something like that in a help file before, but couldn't find it again. Anyway, changed script to:

<trigger priority="4420" id="442">
<pattern>Winning Lotto Numbers are~: (%n) (%n) (%n) (%n) (%n) (%n) (%n) (%n) (%n) (%n)</pattern>
<value>#if (@{%1}) {#var %1 (@{%1} + 1) {0} {lotto}} {#var %1 {1} {0} {lotto}}
#if (@{%2}) {#var %2 (@{%2} + 1) {0} {lotto}} {#var %2 {1} {0} {lotto}}
#if (@{%3}) {#var %3 (@{%3} + 1) {0} {lotto}} {#var %3 {1} {0} {lotto}}
#if (@{%4}) {#var %4 (@{%4} + 1) {0} {lotto}} {#var %4 {1} {0} {lotto}}
#if (@{%5}) {#var %5 (@{%5} + 1) {0} {lotto}} {#var %5 {1} {0} {lotto}}
#if (@{%6}) {#var %6 (@{%6} + 1) {0} {lotto}} {#var %6 {1} {0} {lotto}}
#if (@{%7}) {#var %7 (@{%7} + 1) {0} {lotto}} {#var %7 {1} {0} {lotto}}
#if (@{%8}) {#var %8 (@{%8} + 1) {0} {lotto}} {#var %8 {1} {0} {lotto}}
#if (@{%9}) {#var %9 (@{%9} + 1) {0} {lotto}} {#var %9 {1} {0} {lotto}}
#if (@{%10}) {#var %10 (@{%10} + 1) {0} {lotto}} {#var %10 {1} {0} {lotto}}</value>
</trigger>


Same result.
Reply with quote
hadar
Apprentice


Joined: 30 Aug 2009
Posts: 198
Location: my apt, in california

PostPosted: Thu Jun 07, 2012 2:38 am   
 
this worked for me, seems like something wonky going on with trying to set a var as a number only

Code:

<trigger priority="4420" id="3614">
  <pattern>Winning Lotto Numbers are~: (%n) (%n) (%n) (%n) (%n) (%n) (%n) (%n) (%n) (%n)</pattern>
  <value>#if (@{lotto_}%1) {#var lotto_%1 (@{lotto_}%1+1)} {#var lotto_%1 1 {_nodef} lotto}
#if (@{lotto_}%2) {#var lotto_%2 (@{lotto_}%2+1)} {#var lotto_%2 1 {_nodef} lotto}
#if (@{lotto_}%3) {#var lotto_%3 (@{lotto_}%3+1)} {#var lotto_%3 1 {_nodef} lotto}
#if (@{lotto_}%4) {#var lotto_%4 (@{lotto_}%4+1)} {#var lotto_%4 1 {_nodef} lotto}
#if (@{lotto_}%5) {#var lotto_%5 (@{lotto_}%5+1)} {#var lotto_%5 1 {_nodef} lotto}
#if (@{lotto_}%6) {#var lotto_%6 (@{lotto_}%6+1)} {#var lotto_%6 1 {_nodef} lotto}
#if (@{lotto_}%7) {#var lotto_%7 (@{lotto_}%7+1)} {#var lotto_%7 1 {_nodef} lotto}
#if (@{lotto_}%8) {#var lotto_%8 (@{lotto_}%8+1)} {#var lotto_%8 1 {_nodef} lotto}
#if (@{lotto_}%9) {#var lotto_%9 (@{lotto_}%9+1)} {#var lotto_%9 1 {_nodef} lotto}
#if (@{lotto_}%10) {#var lotto_%10 (@{lotto_}%10+1)} {#var lotto_%10 1 {_nodef} lotto}</value>
</trigger>

_________________
if you build it they will come, assuming that they have not already come to build it
Aardwolf Bootcamp
My youtube channel
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Thu Jun 07, 2012 2:42 am   
 
How do you know that it fired? Your trigger isn't doing anything visible (beyond changing/creating some variables), and you've already said it's not doing that.

Here's an alternative that uses a datarecord variable:

Code:

#trigger {Winning Lotto Numbers are: ([%n%s])} {
  $numbers_drawn = %list(%1," ")
  #forall $numbers_drawn {
    #addkey LottoNumbers %i (%db(@LottoNumbers,%i) + 1)
  }
}
_________________
EDIT: I didn't like my old signature
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Thu Jun 07, 2012 7:50 pm   
 
Aliases and variables cannot start with a number. It's not something "wonky" but by design. I forget the reason for it, though. Anyway, seems that MattLofton's got you covered.
Reply with quote
Llohr
Apprentice


Joined: 17 May 2005
Posts: 108

PostPosted: Wed Jun 20, 2012 2:36 pm   
 
I tried the datarecord method, but nothing happened, not sure what I could have done wrong there.

Also tried hadar's code, which produced some very strange and not useful results, so I took a close look at it. Some of that looks odd to me.
In: "#if (@{lotto_}%2) {#var lotto_%2 (@{lotto_}%2+1)} {#var lotto_%2 1 {_nodef} lotto} "
Should the brackets {} be around the "%2" rather than the "lotto_"?
Should the 1 in the value-if-false section have brackets?
Should the class name (lotto) have brackets? All of the help files show that syntax, and I've been led to believe that cmud is less forgiving of syntax deviation by design.

In any event, this code created 19(!) variables when lotto fired, which included one for each lotto number drawn, and created 9 more equal to the last digit of each lotto number concatenated with the first digit of the following lotto number. Then it set the value to 1 plus the number drawn, rather than 1 for each instance of a particular number. so I'd have a drawing like: Winning Lotto Numbers are: 36 79 34 33 03 94 05 89 49 06
Which would make variables lotto_36, 67, 79, 93, 34, 43, 33, 30, 03, 39, 94, 40, 05, 58, 89, 94, 49, 90, 06, and if any had values, given the variable "lotto_36" the value would be 37.

I've remade the script with about 100 variations and only managed to come up with weird results that don't seem like they should even be possible. Knowing, now, that variables can't begin with numbers, explains some of those results, but thus far nothing I do seems to make this work.
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Wed Jun 20, 2012 4:06 pm   
 
What exactly do you mean by "nothing happened" when you tried the datarecord method? Did you check the value of @LottoNumbers? Did you check whether the trigger had actually fired? Try putting a statement like "#PRINT Lotto Trigger Fired" in the trigger so that you can see whether the trigger is actually firing. If it is not firing, the problem is probably that the trigger pattern is not matching the mud output. Double-check exactly what the mud is sending. An alternative trigger pattern you could try is:
Code:
Winning Lotto Numbers are: ([0-9 ])


I would avoid Hadar's solution; the db variable method is much simpler and keeps all the values nicely contained. Naming variables with implicit concatenation of other values can be tricky. And do you really want all those dozens of variables laying around when it can fit into one variable?
Reply with quote
Llohr
Apprentice


Joined: 17 May 2005
Posts: 108

PostPosted: Wed Jun 20, 2012 4:44 pm   
 
Thank you Rahab, substituting that trigger pattern for the other caused it to fire on the first try.

Now I just wish I knew why it worked. I could never have conceived of that particular syntax, having never seen anything like it in help files. Nor have I been able to find a helpfile for %list, or any reference to it. This method of matching and capturing values without actually having to match each one specifically is completely foreign to me, though it is obviously effective. Where might I look for an explanation of such methods?

As far as I can tell, the first line creates a temporary variable which must have a value of something like 25|23|11|76|etc., 1 number for each drawn. The forall command has a helpfile, so I can see--or believe that I see--that it checks each value in the temporary variable and adds a key for each with a value equal to 1 plus that key, if it already exists. Is that more or less correct?
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Wed Jun 20, 2012 5:46 pm   
 
The trigger pattern is basically placing the entire string "36 79 34 33 03 94 05 89 49 06" into %1.

The %list is described in the help files; you should be able to find it under Functions or by searching on the term. Basically, it converts a string into a stringlist. The code %list(%1," ") basically says 'turn %1 into a stringlist by splitting it on every " " (space character)'. So, it converts "36 79 34 33 03 94 05 89 49 06" into the stringlist {36|79|34|33|03|94|05|89|49|06}

The #FORALL command is an easy way to loop through a stringlist. Each time through the loop, the value %i is set to the next value in the stringlist.

You probably know a little about database variables, but I'll give a quick review. A database variable is a list of key/value pairs. Basically, it is a set like: {key1=value1|key2=value2|...}. In this case, the code is creating a list where the keys are the Lotto numbers and the values are the number of times you have seen that value come up.

The code %db(@LottoNumbers,%i) returns the value associated with the key %i in the db variable @LottoNumbers.

'#ADDKEY varname key value' will set key=value in the variable varname.

So, the code
Code:
#addkey LottoNumbers %i (%db(@LottoNumbers,%i) + 1)

will retrieve the current value of the key %i, add one to it, and set the key to the incremented value. This is inside the #FORALL loop, so it does this for every value in the stringlist, i.e. every value that was captured in the trigger.

Hope that helps!
Reply with quote
hadar
Apprentice


Joined: 30 Aug 2009
Posts: 198
Location: my apt, in california

PostPosted: Thu Jun 21, 2012 3:50 am   
 
BTW never listen to hadar when its past 6pm PST you can never be sure if he is drunk or not
_________________
if you build it they will come, assuming that they have not already come to build it
Aardwolf Bootcamp
My youtube channel
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
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