[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  String problems

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

String problems

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page


Posted by Gaem   (10 posts)  [Biography] bio
Date Thu 07 Dec 2006 10:36 PM (UTC)  quote  ]
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
[Go to top] top

Posted by Nick Gammon   Australia  (18,772 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Thu 07 Dec 2006 10:44 PM (UTC)  quote  ]
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
[Go to top] top

Posted by Gaem   (10 posts)  [Biography] bio
Date Reply #2 on Fri 08 Dec 2006 12:16 AM (UTC)  quote  ]
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?
[Go to top] top

Posted by Nick Gammon   Australia  (18,772 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Fri 08 Dec 2006 12:31 AM (UTC)  quote  ]
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
[Go to top] top

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


1,490 views.

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]