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 ➜ String problems

String problems

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


Posted by Gaem   (10 posts)  Bio
Date Thu 07 Dec 2006 10:36 PM (UTC)
Message
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
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Thu 07 Dec 2006 10:44 PM (UTC)
Message
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 ...

- Nick Gammon

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

Posted by Gaem   (10 posts)  Bio
Date Reply #2 on Fri 08 Dec 2006 12:16 AM (UTC)
Message
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?
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #3 on Fri 08 Dec 2006 12:31 AM (UTC)
Message
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.

- Nick Gammon

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

Posted by Gaem   (10 posts)  Bio
Date Reply #4 on Fri 08 Dec 2006 08:45 AM (UTC)
Message
Thanks again!
That all works fine now.
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.


15,035 views.

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.