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 ➜ Lua ➜ Question regarding tables

Question regarding tables

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


Pages: 1 2  

Posted by Edoren   (31 posts)  Bio
Date Fri 20 Apr 2007 08:14 PM (UTC)

Amended on Fri 20 Apr 2007 08:16 PM (UTC) by Edoren

Message
Hey i have a question regarding using triggers to store wildcards in tables. i have the current triggers:

^You reel in the last bit of line and your struggle is over\. You've landed a (.*) weighing (\d+) (?:pound(?:s)\.|pound(?:s) and (\d+) ounce(?:s)\.)$

^With a final tug, you finish reeling in the line and land a (.*) weighing (\d+) (?:pound(?:s)\!|pound(?:s) and (\d+) ounce(?:s)\!)$

The problem im having is in figuring out how to store the different variables in a database depending on what happens. since its a possibility of having 2 or 3 variables i always end up with an invalid number of arguments if there are only 2. the variables being the name of the fish, the pounds, and ounces. anyone have suggestions on how i should be working this out?
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #1 on Fri 20 Apr 2007 09:34 PM (UTC)

Amended on Fri 20 Apr 2007 09:37 PM (UTC) by Nick Gammon

Message
Yes, use named wildcards. You can also simplify a bit. This one worked for me on all the test cases:


<triggers>
  <trigger
   custom_colour="2"
   enabled="y"


match="^You reel in the last bit of line and your struggle is over\. You've landed a (?P&lt;fish&gt;.*) weighing (?P&lt;pounds&gt;\d+) pounds?(\.| and (?P&lt;ounces&gt;\d+) ounces?\.)$"


   regexp="y"
   send_to="2"
   sequence="100"
  >
  <send>
fish = %&lt;fish&gt;
pounds = %&lt;pounds&gt;
ounces = %&lt;ounces&gt;
</send>
  </trigger>
</triggers>



The important bit is here, withe the &lt; stuff converted (as it would look in the trigger dialog):


You've landed a (?P<fish>.*) weighing (?P<pounds>\d+) pounds?(\.| and (?P<ounces>\d+) ounces?\.)


The named wildcards are in the form (?P<name> match) and the other thing you can do to simplify is use something like "pounds?" this is because the "?" after the "s" means "zero or one of them", which is fine for detecting singular/plural.

- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #2 on Fri 20 Apr 2007 09:41 PM (UTC)
Message
As for storing the data, if you use "send to script" this works fine:


lastfish = {}

lastfish.fish = "%<fish>"
lastfish.pounds = %<pounds>
lastfish.ounces = tonumber ("%<ounces>") or 0



The "or 0" handles the possibility that the ounces might not be there.

- Nick Gammon

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

Posted by Edoren   (31 posts)  Bio
Date Reply #3 on Fri 20 Apr 2007 10:39 PM (UTC)
Message
i keep getting this:

Line 110: Elements terminated out of sequence, expected </ounces>, got </send> (problem in this file)

fish=%<fish>
pounds=%<pounds>
ounces=%<ounces>
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #4 on Fri 20 Apr 2007 11:26 PM (UTC)
Message
See: http://mushclient.com/pasting

If you copy and paste my example between <triggers> and </triggers> it works.

- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #5 on Fri 20 Apr 2007 11:28 PM (UTC)
Message
If you are pasting into a plugin you have to change < to &lt; and > to &gt;

- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #6 on Sat 21 Apr 2007 06:03 AM (UTC)
Message
Since you always get the trailing period, it could look better as:


You've landed a (?P<fish>.*) weighing (?P<pounds>\d+) pounds?( and (?P<ounces>\d+) ounces?)?\.


- Nick Gammon

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

Posted by Edoren   (31 posts)  Bio
Date Reply #7 on Sat 21 Apr 2007 03:32 PM (UTC)
Message
well that helps me out with that. a question though, with that script i end up losing the data everytime its triggered. im thinking i'd be better off using my start and stop alias to create/print. also i'd be using the triggers to insert. im using a plugin atm, so whenever i try printing/inserting it doesnt work from functions. im guessing im just missing something to call the table before inserting. any suggestions?
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #8 on Sat 21 Apr 2007 09:29 PM (UTC)
Message
It is pretty hard to help from that description. Perhaps posting the plugin if it is reasonably short, and expaining what it is doing, and what you expect it to do.

- Nick Gammon

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

Posted by Edoren   (31 posts)  Bio
Date Reply #9 on Sat 21 Apr 2007 09:53 PM (UTC)

Amended on Sat 21 Apr 2007 09:58 PM (UTC) by Edoren

Message
<trigger
   enabled="y"
   match="^You reel in the last bit of line and your struggle is over\. You've landed a (?P&lt;fish&gt;.*) weighing (?P&lt;pounds&gt;\d+) pounds?(\.| and (?P&lt;ounces&gt;\d+) ounces?\.)$"
   regexp="y"
   script="reel_stop"
   sequence="100"
  >
  </trigger>

  <trigger
   enabled="y"
   match="^With a final tug, you finish reeling in the line and land a (?P<fish>.*) weighing (?P<pounds>\d+) (pounds?(\!| and (?P<ounces>\d+) ounces?\!))$"
   regexp="y"
   script="reel_stop"
   sequence="100"
  >
  </trigger>

<alias
 match="^fish (.+)$"
 regexp="y"
 enabled="y"
 script="fishing"
 sequence="100"
 >
 </alias> 

function fishing (name, line, wildcards)
  local s=(wildcards[1])
  if s == "on" then
   Send ("unwield left")
   Send ("unwield right")
   Send ("put 2 shortsword in bag")
   Send ("get pole from kitbag")
   Send ("get bucket from kitbag")
   Send ("wield pole")
   else if s == "off" then
    world.Execute ("rl")
    Send ("unwield pole")
    Send ("put pole in kitbag")
    Send ("put bucket in kitbag")
   else
    Note ("I'm sorry, that didn't work")
  end --else if
 end --if
end --fishing on or off?

function reel_stop ()
 EnableTrigger ("reel_caught", false)
 Send ("put fish in kitbag")
 world.Execute ("bh")
end --stop reel


i want to just use the fish alias so that when its set on, it will create the table. then the triggers for inserting into the table. then once its eventually done i want to use the fish alias again to print the actual data. the reel_stop function is called when the fish is caught for both triggers, and the fishing function is called when i start and finish.

obviously there are more triggers/alias/functions but these are the only one's im referring to.
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #10 on Sun 22 Apr 2007 12:32 AM (UTC)
Message
Well for a start, your script (if this is inside a plugin) needs to be inside <script> tags, like this:


<script>

function reel_stop ()
 EnableTrigger ("reel_caught", false)
 Send ("put fish in kitbag")
 world.Execute ("bh")
end --stop reel

---> and other functions

</script>


Next, as I tried to explain before, inside a plugin you need to convert < to &lt;. See how inside the plugin there are things like: </trigger>? Well, in your plugin it sees <fish> and tries to interpret that as some sort of XML keyword.

You have to make it &lt;fish&gt;

You have it in your first trigger, but not the second one.

- Nick Gammon

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

Posted by Edoren   (31 posts)  Bio
Date Reply #11 on Sun 22 Apr 2007 01:02 AM (UTC)
Message
i left out the script/triggers tags because i copy/pasted them from the plugin since you wanted a visual of what i was talking about. as to the second trigger i was using the right one i apparently copied from the test version of the plugin where i was just checking things out so that's not a problem.

anyway, the real question i had was in regards as how i can access the table if i created it in the alias function to access in the trigger function for storing and then print it again from the alias function.
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #12 on Sun 22 Apr 2007 01:18 AM (UTC)
Message
Tables are shared between all functions in the script space. Can you show what you are doing a bit more? I am having trouble understanding what the problem is.

- Nick Gammon

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

Posted by Edoren   (31 posts)  Bio
Date Reply #13 on Sun 22 Apr 2007 03:18 AM (UTC)

Amended on Sun 22 Apr 2007 03:25 AM (UTC) by Edoren

Message
function fishing (name, line, wildcards)
  local s=(wildcards[1])
  if s == "on" then
   Send ("unwield left")
   Send ("unwield right")
   Send ("put 2 shortsword in bag")
   Send ("get pole from kitbag")
   Send ("get bucket from kitbag")
   Send ("wield pole")
   lastfish = {}
   else if s == "off" then
    world.Execute ("rl")
    Send ("unwield pole")
    Send ("put pole in kitbag")
    Send ("put bucket in kitbag")
    table.foreach (lastfish, print)
   else
    Note ("I'm sorry, that didn't work")
  end --else if
 end --if
end --fishing on or off?


function reel_stop (name, line, wildcards)
 EnableTrigger ("reel_caught", false)
 Send ("put fish in kitbag")
 world.Execute ("bh")
 lastfish.fish = wildcards[1]
 lastfish.pounds = wildcards[2]
 lastfish.ounces = tonumber "(wildcards[3])" or 0
end --stop reel


I received this line:

You reel in the last bit of line and your struggle is over. You've landed a walleye weighing 15 pounds and 11 ounces.

it printed:

fish walleye
ounces 0
pounds 15


Actually this is a lot better than before. When i was first trying it wouldn't even print anything since it was coming out nil. Must have messed something else up that i fixed. But now i get everything except for the ounces correctly atm. The second thing is if i want to insert these into the table instead with table.insert () how would i go about it? I tried testing it out but it came back nil with the same values as above. More than likely i dont have the exact syntax for table.insert with the strings. At the moment i'm really just experimenting and learning about tables and more about functions. Anyway, my last question is how i can print the name/pound/ounces on the same line, so that every time a trigger goes off it will print those 3 before making a new line and sending the next.
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #14 on Sun 22 Apr 2007 04:55 AM (UTC)
Message
You need to think about what you are doing here. In my example I was using "send to script" when I gave this code:


lastfish.fish = "%<fish>"
lastfish.pounds = %<pounds>
lastfish.ounces = tonumber ("%<ounces>") or 0


So, if the fish was "flounder", pounds were "22" and ounces were "7", this is what the script would have seen:


lastfish.fish = "flounder"
lastfish.pounds = 22
lastfish.ounces = tonumber ("7") or 0


That is all fine. But you have moved it into a trigger function and substituted wildcards [1] etc. So far so good. But for ounces you left the quotes in, so this is what you have:


lastfish.ounces = tonumber "(wildcards[3])" or 0


However it can't convert the words "(wildcards[3])" into a number. You have put a variable name into quotes, that won't work. You need instead:


lastfish.ounces = tonumber (wildcards[3]) or 0


The other thing to consider is, we have gone to the trouble of naming the wildcards, why not use the names? Like this:


lastfish.fish = wildcards.fish
lastfish.pounds = wildcards.pounds
lastfish.ounces = tonumber (wildcards.ounces) or 0


That make it much easier to read, and you don't get confused about what each number means.

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


57,296 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

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.