Setting multiple variables?

Posted by Deiwos on Thu 28 Oct 2010 05:42 AM — 11 posts, 34,474 views.

#0
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?
USA #1
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.
#2
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.
Amended on Thu 28 Oct 2010 06:19 AM by Deiwos
USA #3
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")
Amended on Thu 28 Oct 2010 06:21 AM by Twisol
#4
Oh, awesome, thank you!
USA #5
No problem. :)
Australia Forum Administrator #6
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"

USA #7
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.
Australia Forum Administrator #8
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.
USA #9
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.
#10
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.