String problems

Posted by Gaem on Thu 07 Dec 2006 10:36 PM — 5 posts, 20,464 views.

#0
I'm sure this question has probably been asked before, but I can't seem to find an answer in the usual documentation places.

I've run into a problem where I'm trying to compare two strings (from variables in MUSH), but modifying one with arithmetic.

eg.

function f1 (name, line, wildcards)

local variable1 = GetVariable("variable1")
local variable2 = GetVariable("variable2")
local variable3 = GetVariable("variable3")


if variable1 < 0.85 * variable2 then (here I have a problem, since it says I can't do arithmetic on a string)
Note ("yay!")
end

end -- function


Also, what is the syntax in lua for conditioning on 2 variables? So, as above, but requiring variable3 = 1 as well as variable1 < 0.85 * variable2.

Thanks
Australia Forum Administrator #1
Use "tonumber", see:

http://www.gammon.com.au/scripts/doc.php?general=lua_base

eg.


if tonumber (variable1) < 0.85 * tonumber (variable2) then ,,,


As for multiple conditions, you can do:


if a < b and c > d then ...
#2
Thanks!
The first part works fine, but I get this error whenever I try to do if statements with 2 parts;

compile error
[string "Script file"]:63: 'then' expected near '='

I was using

if tonumber(var1) < 0.85 * tonumber(var2) and var3 = 1 then

as the line.
Any ideas as to what I'm doing wrong?
Australia Forum Administrator #3
Equality test is == not =.

Inequality is ~= (in Lua).

So yours should read:


if tonumber(var1) < 0.85 * tonumber(var2) and var3 == 1 then


The "=" operator is assignment. Thus you can write this:


a = b == 2


This makes a true if b is equal to 2, false otherwise.
#4
Thanks again!
That all works fine now.