Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
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 > 0 then
found = vbTrue
Send "get '%1' " & k ' eg. get chicken sack
count = count - 1
if count > 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 |
|