Local Variables

From Scriptwiki
Revision as of 15:17, 10 November 2005 by Microbe (talk | contribs) (minor tweaking)

Jump to: navigation, search

Local variables are variables that exist only for the duration of the script in which they are created and can only be accessed from within that script. They can be created with the /var command.

Var creates the local variable in the current routine and can only be referenced from inside this routine.

The difference between /var and set is that var creates local variable what only exists while script is executed. Set sets value to variable and stores it there until it's unset. Local variables don't overwrite variables set with "set" command values.

You can set multible variables with one set

var %x = foo, %y = bar

Usage of = in syntax is usually optional, but highly recommended

var %x foo

You can use /var -s to make a variable show the result when a value is set.

Examples

First, let's create an alias that's used later on.

alias var_example { var -s %x foo | set -s %y bar }

Now, let's play around.

; set values to variables with set so values exists until they're unset
set %x 10
set %y %x
; value from %x is copied to %y

; call of your own alias
var_example
; displays
; * Set %x to foo
; * Set %y to bar

; now, let's display %x and %y values
echo -a %x
; returns 10
echo -a %y
; returns bar

As we can see, %x still holds its original value because /var only creates a local variable, but %y was overwritten with /set.

Contributed by Tovrleaf