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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Setting multiple variables?

Setting multiple variables?

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


Posted by Deiwos   (6 posts)  [Biography] bio
Date Thu 28 Oct 2010 05:42 AM (UTC)
Message
I'm not sure if this is the right way to do it, but I want to create a script that takes three inputs: 'Elixir name' 'Number to be made' 'Vial type to put them into', so I hoped it could be utilized with an alias of a format like this:
FILL HEALTH 5 VIAL
And then take %1 as the elixir, %2 as the number and %3 as the type. But I'm not sure how to make the script (in Lua) capture each of these separately and thought I might need to make an internal variable for each, which would then be captured by GetVariable().

The only problem being that I don't know if it's possible to create more than one variable at once. Am I wrong track? Should I be asking in the Lua subforum?
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #1 on Thu 28 Oct 2010 05:44 AM (UTC)
Message
Is there anything you've tried that you can post here? It sounds like what you want should be possible, but I'm not sure on the specifics of what it is you want.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Deiwos   (6 posts)  [Biography] bio
Date Reply #2 on Thu 28 Oct 2010 06:06 AM (UTC)

Amended on Thu 28 Oct 2010 06:19 AM (UTC) by Deiwos

Message
I don't even know how to begin. I know of putting a variable name in the Variable spot of the add alias window will create a variable called that, but I think I need three separate things.

To (hopefully) clarify a bit I want three variables:
elixirname
elixirnum
vialtype

I hope the alias can be 'fill *' and I can type 'fill health 1 black-walnut', so I can have a script that does, for example, 'Pull list of herbs needed based on elixirname, for i = 1 to elixirnum, then pour into vialtype'.

But I don't know how to make a script get what I type after 'fill ' and separate that into three variables.
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #3 on Thu 28 Oct 2010 06:19 AM (UTC)

Amended on Thu 28 Oct 2010 06:21 AM (UTC) by Twisol

Message
Oh, I see. Your alias should be "fill * * *", and the script can be:

local elixirname = "%1"
local elixirnum = tonumber("%2")
local vialtype = "%3"

Then you can use those variables however you want in the rest of the Send script. They'll disappear once it's finished unless you save them somewhere else (like using SetVariable, for example).

If you want to put them into MUSHclient variables straight away, you can use this:
SetVariable("elixirname", "%1")
SetVariable("elixirnum", "%2")
SetVariable("vialtype", "%3")

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Deiwos   (6 posts)  [Biography] bio
Date Reply #4 on Thu 28 Oct 2010 06:21 AM (UTC)
Message
Oh, awesome, thank you!
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #5 on Thu 28 Oct 2010 06:22 AM (UTC)
Message
No problem. :)

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #6 on Thu 28 Oct 2010 06:25 AM (UTC)
Message
Twisol said:

local elixirname = "%1"
local elixirnum = tonumber("%2")
local vialtype = "%3"




I'd drop the word "local" personally. That might not be saved for as long as you expected.




elixirname = "%1"
elixirnum = tonumber("%2")
vialtype = "%3"


- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #7 on Thu 28 Oct 2010 06:28 AM (UTC)
Message
Nick Gammon said:
I'd drop the word "local" personally. That might not be saved for as long as you expected.

Right, I mentioned that. I think it's better to use local, because you might accidentally use the same variable in another script and/or overwrite it. If you want to save it, you can always be explicit about it.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #8 on Thu 28 Oct 2010 06:37 AM (UTC)
Message
I suppose it depends what he means by "capture" but your use of local means the data has to be used inside that alias, so he may as well have stuck to %1, %2 etc.

To "capture" it, for future use, then "local" is not appropriate.

Twisol said:

... you might accidentally use the same variable in another script and/or overwrite it ...


Well you could say exactly the same thing about SetVariable. If you use the same variable in another place you might overwrite it.

Twisol said:

They'll disappear once it's finished unless you save them somewhere else (like using SetVariable, for example).


If you remove "local" then you don't need to do the mucking around with SetVariable. And the problem with overwriting has neither got worse nor better.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #9 on Thu 28 Oct 2010 06:56 AM (UTC)
Message
Nick Gammon said:
To "capture" it, for future use, then "local" is not appropriate.

True. I read it as "capturing from the alias", as in how %1, %2, %3 etc. are "captures".

Nick Gammon said:
Well you could say exactly the same thing about SetVariable. If you use the same variable in another place you might overwrite it.

Which is exactly why I prefer 'local'. :)

Nick Gammon said:
Twisol said:

They'll disappear once it's finished unless you save them somewhere else (like using SetVariable, for example).


If you remove "local" then you don't need to do the mucking around with SetVariable. And the problem with overwriting has neither got worse nor better.

I'm going to have to disagree with that. One very common slip-up is to name a variable "string" or "table". It's always best to keep variables in the smallest scope you can reasonably support.

If Deiwos needs to keep the variables beyond the alias script's lifetime, then by all means, remove 'local'. It's an excellent safe default though.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by LezChap   (36 posts)  [Biography] bio
Date Reply #10 on Thu 28 Oct 2010 10:26 AM (UTC)
Message
There's also the thought that local variables (even if they're local for the whole global environment) are faster/more efficient than global, non-local variables...at least in Lua.

The efficiency probably isn't something you're going to notice in these small scripts you use...but some people like to be as optimized as possible.
[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.


21,985 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

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

[Best viewed with any browser - 2K]    [Hosted at HostDash]