Page 1 of 1

Comparing # to string error.

Posted: Fri Sep 25, 2009 11:28 pm
by Xixity
I'm doing something wrong obviously but I'm not sure what. I tried both patterns but I get the same error.
Could someone explain this to me?

Patterns:
You bleed (\d+) health.
You bleed (\w+) health.

Script:
clotamt=((matches[2]/20)-2)
if clotting == 1 then
if matches[2] > 80 then
send("clot " ..clotamt)
end
end

Error msg:
LUA error running script You bleed (\w+) health. (Trigger627) ERROR:Lua error:[string "function Trigger627()..."]:4: attempt to compare number with string

Re: Comparing # to string error.

Posted: Fri Sep 25, 2009 11:46 pm
by Heiko
The error simply says that you can't do mathematical calculations on a string e.g. the expression ("Tony"/20)-3 makes no sense.
The capture pattern (\d+) is fine, but the error is in the script. Matches[] is a string table. Lua has no way to know if your number capture (\d+) e.g. "345" has to be interpreted as a string "345" or the number 345. This is why you need to explicitely use the tonumber() conversion function if you want to use the content of matches[] as numbers.

clotamt=((tonumber(matches[2])/20)-2)

Re: Comparing # to string error.

Posted: Sat Sep 26, 2009 12:56 am
by Xixity
ed:You bleed 90 health.

Trigger name=You bleed (\d+) health.(You bleed (\d+) health.) matched.
capture group #0 = <You bleed 90 health.>
capture group #1 = <90>
LUA OK script You bleed (\d+) health. (Trigger627) ran without errors
new line arrived:Health Lost: 90

says the trigger is matching and that the script is running without errors, but it's not doing what it's supposed to.

clotamt=((tonumber(matches[2])/20)-2)
if clotting == 1 then
if tonumber(matches[2]) > 80 then
send("clot " ..clotamt)
end
end

Re: Comparing # to string error.

Posted: Sat Sep 26, 2009 2:45 am
by Caled
Sorry, not sure what the problem with your script is (perhaps just a math error?), but this is how I'm doing it. Maybe this will help.

Code: Select all

Trig Pattern: ^You bleed (\d+) health\.$
Script:
if (tonumber(matches[2]) > 29) then doclot(tonumber(matches[2])/40) end


function doclot(num)
	fg("red")
	bg("black")
	echo("<--- Gonna Clot")
	if num < 0 then num = 0 end
	while num > 0 do
		sendr("clot")
		num = num - 1
	end
end

Re: Comparing # to string error.

Posted: Sat Sep 26, 2009 8:24 am
by Heiko
Xixity wrote:ed:You bleed 90 health.

Trigger name=You bleed (\d+) health.(You bleed (\d+) health.) matched.
capture group #0 = <You bleed 90 health.>
capture group #1 = <90>
LUA OK script You bleed (\d+) health. (Trigger627) ran without errors
new line arrived:Health Lost: 90

says the trigger is matching and that the script is running without errors, but it's not doing what it's supposed to.

clotamt=((tonumber(matches[2])/20)-2)
if clotting == 1 then
if tonumber(matches[2]) > 80 then
send("clot " ..clotamt)
end
end
Most likely your global variable clotting is not equal to 1. To debug your script you need to print the values of the respective variables to see where the error is. Here you go:

Code: Select all

-- declare a number variable c to hold the number caught in capture group #1
local c = tonumber( matches[2] ) 
-- declare a second number to hold the value of your calculation and print the result
local clot = c / 20 - 2
echo( "c=" .. c .. " clot=" .. clot .. " clotting=" .. clotting .. "\n" )
if clotting == 1 then
    if clot > 80 then
        send( "clot " .. clot )
    end
end
Note the difference between local and global variables. If you use the keyword "local" in the definition of a variable this variable is only defined in the direct context e.g. it is local to a function or a block etc. Global variables are defined without the keyword local. They are available everywhere. In the script above local variables are not necessary, but it's good practice in Lua to keep the global name space clean. Have a look at the Lua variable tutorial posted in the global forum announcement thread.