Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ a trigger to add to a variable

a trigger to add to a variable

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Keia   (2 posts)  Bio
Date Fri 16 Jul 2004 03:41 PM (UTC)
Message
<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?
Top

Posted by Flannel   USA  (1,230 posts)  Bio
Date Reply #1 on Fri 16 Jul 2004 10:10 PM (UTC)

Amended on Fri 16 Jul 2004 10:51 PM (UTC) by Flannel

Message
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.

~Flannel

Messiah of Rose
Eternity's Trials.

Clones are people two.
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #2 on Fri 16 Jul 2004 10:38 PM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Keia   (2 posts)  Bio
Date Reply #3 on Sat 17 Jul 2004 01:57 AM (UTC)
Message
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.
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #4 on Sat 17 Jul 2004 04:22 AM (UTC)

Amended on Sat 17 Jul 2004 04:24 AM (UTC) by Nick Gammon

Message
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".

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


14,218 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.