Register to post in forums, or Log in to your existing account
 

 Related 
Contents
Variables
  Local Variables
  Variable Types
  Indirect Variables
Related Links:
  New Features
  %match
  %regex
  %yesno
  #ALIAS
  #LOCAL
  #VARIABLE
  New Commands
  Introduction to Aliases
  Introduction to Variables
  Variables
  Aliases
  Triggers
Local Variables [[cmud_local]] 
Local variables are temporary variables that can be used in your scripts which provide huge speed benefits. Like normal variables, a Local Variable contains a value. However, Local Variables are not saved in your package and cannot be edited using the Package Editor.

Local Variables only exist within the script block that they are defined. Local Variables start with a $ character instead of a @ character.

To create a Local variable, use the #LOCAL command, or simple initialize the variable with a value using the normal variable assignment syntax:

$localvar = value

Note the difference in syntax between this assignment and a normal variable assignment. Normal variable assignment doesn't include the @ character. The @ character is only used when retrieving the value of a normal variable. For Local variables, the $ character is used for both defining and retrieving a value.

So, to display the value of a Local variable, you could do this:

#SHOW $localvar

Variable Scope

Local variables are only accessible within the "script block" that the variable was defined. A "script block" is typically a set of commands and statements surrounded by the {} characters. Here is an example:

Code:
$toplevel = "main"
#SHOW "Outer level: " $toplevel
#IF (true) {
  $insideIf = "inside"
  #SHOW "Inside IF statement: " $insideIf
  #SHOW "Can still access top level: " $toplevel
  }
#SHOW "Back to top level: " $toplevel
#SHOW "But InsideIf no longer exists: " $insideIf


If you try to execute this on the command line, you will get an error telling you that $insideIf is not defined in the last statement. You are not allowed to reference an undefined local variable.

Notice that the $insideIf Local variable only exists within the "script block" of the #IF statement. Whereas the $toplevel Local variable created in the main outer part of the script is accessible anywhere in the script. Once the script is done executing, then $toplevel no longer exists either.

Local variables are excellent for temporary storage within your script where you don't need the variable at a later time. And because they only exist within memory and within the currently running script, they are much much faster than normal variables.

 User comments 
darmir: Wed Nov 03, 2010 2:28 am    

Zugg,
I see that there is a mis-spelling in a sentence in this help file.
This sentence:
Not the difference in syntax between this assignment and a normal variable assignment.
should be:
Note the difference in syntax between this assignment and a normal variable assignment.
Tech: Wed Nov 03, 2010 3:19 pm    

Corrected.
KimberlyDeborah: Tue Nov 05, 2013 7:12 am    

In programming local variable are important it will help to contain the data with particular name and has particular position i memory with that name
Viewer Comments [3 - Post your comments]

Jump to:  

© 2009 Zugg Software. Hosted by Wolfpaw.net