Trigger gives error message "attempting to concatenate nil value". Why?

Posted by Agarwain on Tue 15 Nov 2011 04:27 AM — 6 posts, 32,356 views.

#0
Hello,

I'm trying to do something nifty in Lua, and failing quite miserably. Essentially, I want to create a table of values from a trigger that will examine something called waxlist, and store the wax number, the name of the person who it's of, and the form of the wax. The regexp seems to be fine, because when I finally do type 'waxlist', all hell breaks loose. The trigger:

<triggers>
<trigger
enabled="y"
expand_variables="y"
ignore_case="y"
keep_evaluating="y"
match="^[\d+]?x?\s?&quot;wax(\d+)&quot;.*Image: (.*) \((\d+) bondings/?(.*)?\)\.$"
name="waxlist"
omit_from_output="y"
regexp="y"
send_to="12"
sequence="100"
>
<send>waxlist [item] = { wax_id = "wax" .. tonumber( %2 ), name = "%3", bondings = tonumber( %4 ), form = "%5"}
waxlisted = 1
item = item + 1</send>
</trigger>
</triggers>


And the error:

Run-time error
World: Legends2
Immediate execution
[string "Trigger: waxlist"]:1: attempt to concatenate a nil value
stack traceback:
[string "Trigger: waxlist"]:1: in main chunk

I am thoroughly confused as to why this is. Any help would be greatly appreciated.
USA #1
If it has this error message, then there's a place where one of your variables or wildcards has just what it says, a nil value, and can't be concatenated with string.

It's just the rule: you can't concatenate a string (this is using the ..) with nothing.

Based on the error line, it looks like this is an issue with something in your waxlist table, specifically with the wax_id = "wax" .. tonumber( %2 ) entry. For some reason or another your wildcard %2 does not have a value and therefore cannot be converted to a number and therefore cannot be concatenated with the string "wax".

This could happen if it was an optional wildcard that wasn't used every time you used the command.

Might want to do some checking such as:

wax_id_num = %2
if wax_id_num == nil
  wax_id_num = 0
end -- if

wax_id = "wax" .. tonumber(wax_id_num)
Amended on Tue 15 Nov 2011 05:25 AM by Daniel P
#2
It happens every time the command shows up. A normal iteration looks like:

"wax12345" Image: Agarwain (20 bonds/human).

Am I capturing the %2 (which should be '12345' in the example above) incorrectly then?
USA Global Moderator #3
What makes you think that 12345 is in %2 instead of %1?
#4
Hmm! i thought [\d+]?x?\s? would count as %1. Guess not.
Australia Forum Administrator #5
Only if they are inside (round) brackets are they "captured".

So for example:


(foo) dog (bar) cat


%1 would be "foo" and %2 would be "bar". The other words are not captured. This applies whether things are fixed or variable, eg.


([\d+]) dog (.*) cat



Template:regexp
Regular expressions
  • Regular expressions (as used in triggers and aliases) are documented on the Regular expression tips forum page.
  • Also see how Lua string matching patterns work, as documented on the Lua string.find page.


Square brackets are used for "sets of things". Are you really looking for a single digit or a plus sign? Or do you mean:


\d+


or, to capture it:


(\d+)


That's not the same thing. What you have is the set of digits, plus "+", once.