Difference between revisions of "Local Variables"

From Scriptwiki
Jump to: navigation, search
m (minor tweaking)
Line 3: Line 3:
 
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.
  
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.
+
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
 
You can set multible variables with one set
 
  var %x = foo, %y = bar
 
  var %x = foo, %y = bar
  
Usage of = in syntax is optional
+
Usage of = in syntax is usually optional, but highly recommended
 
  var %x foo
 
  var %x foo
  
 
You can use /var -s to make a variable show the result when a value is set.
 
You can use /var -s to make a variable show the result when a value is set.
  
== Example ==
+
== Examples ==
  
First, let's create [[Aliases|alias]] needed later on.
+
First, let's create an [[Aliases|alias]] that's used later on.
 
  alias var_example { var -s %x foo | set -s %y bar }
 
  alias var_example { var -s %x foo | set -s %y bar }
  
Line 23: Line 23:
 
  set %x 10
 
  set %x 10
 
  set %y %x
 
  set %y %x
  ; value fro %x is copyed to %y (for users from with previous knowledge from scripting)
+
  ; value from %x is copied to %y
 
   
 
   
 
  ; call of your own alias
 
  ; call of your own alias
Line 34: Line 34:
 
  [[echo]] -a %x
 
  [[echo]] -a %x
 
  ; returns 10
 
  ; returns 10
  [[echo]] -a %y
+
  echo -a %y
 
  ; returns bar
 
  ; returns bar
  
As we can see, %x still holds it origial value because /var creates only local variable, but %y was overwritten with /set.
+
As we can see, %x still holds its original value because /var only creates a local variable, but %y was overwritten with /set.
  
 
{{Author|Tovrleaf}}
 
{{Author|Tovrleaf}}
  
 
[[Category:Variables]]
 
[[Category:Variables]]

Revision as of 15:17, 10 November 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 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