a trigger to add to a variable

Posted by Keia on Fri 16 Jul 2004 03:41 PM — 5 posts, 20,145 views.

#0
<triggers>
<trigger
custom_colour="2"
enabled="y"
match="You put a * in a *."
name="container"
send_to="9"
sequence="100"
variable="container"
>
<send>%2</send>
</trigger>
</triggers>

then

<aliases>
<alias
match="^get (.*?) from$"
enabled="y"
expand_variables="y"
regexp="y"
keep_evaluating="y"
sequence="100"
>
<send>get %1 @container</send>
</alias>
</aliases>

both trigger and alias work but not how i want
each time the trigger sets off it clears the variable
instead of adding it to the list like a want. can anyone help?
USA #1
what list?
You want to be able to put something in a container, and then store the container name in a variable, so you can get stuff out of it easier? It looks like it does that.

Maybe I misunderstand, I still dont know what "list" youre talking about.

Nick? Why did you change conversion of * to (.*?), doesnt seem like itd do what people wanted.
Amended on Fri 16 Jul 2004 10:51 PM by Flannel
Australia Forum Administrator #2
The .*? means a non-greedy wildcard. See:

http://www.gammon.com.au/mushclient/regexp.htm

The difference is in a case like this:

Nick says John says hello.

Now if you match that with:

* says *

You would expect that wildcard 1 would be "Nick" and wildcard 2 would be "John says hello".

However without the "?" the first wildcard is greedy, and you would actually get wildcard 1 as "Nick says John" and wildcard 2 as "hello".




As for the original question, by "list" I presume you are keeping a list of things in the bag? This is one way to do it ...


<triggers>
  <trigger
   custom_colour="2"
   enabled="y"
   match="You put * in a *."
   send_to="12"
   sequence="100"
  >
  <send>ArrayCreate "container"
ArrayImport "container", GetVariable ("container"), ","

count = ArrayGet ("container", "%1")
if IsEmpty (count) Then
  count = 1
else
  count = count + 1
end if

ArraySet "container", "%1", count
SetVariable "container", ArrayExport ("container", ",")</send>
  </trigger>
</triggers>


What this trigger will do is maintain an array of contents of the container (bag), and when you put another one in, check to see if there is one already, and if so add 1 to it, otherwise it records you as having one of them. Then it exports the list into the variable "container".

Thus, after a couple of tries, that variable might contain:

a fish,3,a toad,2

That is, 3 fish, 2 toads.

However it isn't too clear what you are doing with your alias. Do you have more than one container, is that it? Are you trying to cycle through all of them to find the thing you want? If that is the case, my trigger would need to be more complex, instead of just "container" the name of the container would need to be used. Basically you would replace the word "container" by %2 in the trigger, that should do it.

Then the alias would need to cycle through all the containers, to find the item. This is getting more complex, but it could be done.
#3
actually list referred to the variable container
what i am tring to do is make a list of the bags/pack/saddlebags i have so i can type get xxx from and it will check for the item in all the containers in the variable "container" i get it to almost work right with what i have but it replaces the old variable each time.
command: put torch pack
see: you put a torch in a backpack
match: you put a * in a *
and will add the second wildcard to the variable instead of replacing the old variable.
Australia Forum Administrator #4
I thought you might have meant that. :)

Adding a second wildcard to the variable would be easy enough, but you still have the problem of finding them later. For instance you could have checked "expand variables" and used this:

send: @container, %1

However you will still have lots of problems doing that. I have revised the trigger to store into multiple containers, basically using an "array within an array". Here is the trigger:


<triggers>
  <trigger
   custom_colour="2"
   enabled="y"
   match="You put a * in a *."
   send_to="12"
   sequence="100"
  >
  <send>'
'  all containers (container of containers)
'
ArrayCreate "containers"
ArrayImport "containers", GetVariable ("containers"), "*"
'
'  this particular one (eg. bag, backpack)
'
ArrayCreate "%2"  ' which container
ArrayImport "%2", ArrayGet ("containers", "%2"), ","


count = ArrayGet ("%2", "%1")
if IsEmpty (count) Then
  count = 1
else
  count = count + 1
end if

'
'  put count back into this container
'
ArraySet "%2", "%1", count
'
'  put all items in this container into the containers array
'
ArraySet "containers", "%2", ArrayExport ("%2", ",")
SetVariable "containers", ArrayExport ("containers", "*")</send>
  </trigger>
</triggers>



Now your variable "containers" will look like this:

bag*fish,2*bottle*milk,1*bucket*toad,2*sack*wand,2

In this case you have 4 containers: bag, bottle, bucket, sack. The asterisk delimits containers, and the comma delimits items within the container.

In the bag is: 2 X fish
In the bottle is: 1 X milk
In the bucket is: 2 X toad
In the sack is: 2 X wand

Then this alias searches each container until it finds one of what you want, and decrements the count "in stock" by one:


<aliases>
  <alias
   match="get *"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>'
'  all containers (container of containers)
'
ArrayCreate "containers"
ArrayImport "containers", GetVariable ("containers"), "*"
found = vbFalse

'
'  get all containers we know of
'
contList = ArrayListKeys ("containers")
If Not IsEmpty (contList) Then
  
  ' 
  ' check each container
  '
  For Each k In contList 
    ArrayCreate k
    ArrayClear k
    ArrayImport k, ArrayGet ("containers", k), ","

    '
    '  is the wanted item in this container?
    '
    count = ArrayGet (k, "%1")

    if count &gt; 0 then
      found = vbTrue
      Send "get '%1' " &amp; k  ' eg. get chicken sack
      count = count - 1
      if count &gt; 0 then
        ArraySet k, "%1", count
      else
        ArrayDeleteKey k, "%1"  ' none left - delete from container
      end if  ' none left
      '
      '  put all items in this container into the containers array
      '
      ArraySet "containers", k, ArrayExport (k, ",")
      SetVariable "containers", ArrayExport ("containers", "*")
      Exit For   ' don't keep looping
    end if ' item found

  Next  ' container

End If  ' having any containers

if not found then
   ColourNote "white", "red", "%1 not found in any container"
end if

</send>
  </alias>
</aliases>


Now you might type "get fish" and it will send "get 'fish' bag".