Difference between revisions of "Local Variables"

From Scriptwiki
Jump to: navigation, search
 
Line 1: Line 1:
== Local Variables ==
+
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.
 
Var creates the local variable in the current routine and can only be referenced from inside this routine.

Revision as of 18:00, 25 August 2005

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 doesn'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 optional

var %x foo

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

Example

First, let's create alias needed 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 fro %x is copyed to %y (for users from with previous knowledge from scripting)

; 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 it origial value because /var creates only local variable, but %y was overwritten with /set.