Taken from the tip of the week for October 12, 2007 this article explains how variables in Lasso can be used to store temporary values. Topics include creating variables, outputting variables, modifying variables in-place, and some variables tools.
A variable is a place to hold a value temporarily. Each variable has a name and a value. In Lasso the name of a variable is a string and the value can be of any type. The concept of a variable is rooted in mathematical notation such as "x = 5" or "y = x + 5". The nice thing about Lasso versus your Algebra homework is that Lasso will perform the arithmetic for you.
Each page load in Lasso has its own set of page variables. These variables can be accessed only from within the current page execution and are destroyed when the page results are sent to the browser. Lasso also has global variables which are available server-wide and local variables which are only available within the context they were created. These are described in previous tips of the week (listed in More Information below).
A variable in Lasso is created using the [Variable] tag or the abbreviation [Var]. The tag is normally used to define the name and initial value for the variable. The following tags create a variable named "x" with an initial value of 5 and a variable named "myString" with an initial value of "The quick brown fox".
If the [Var] tag is called with just a variable name then it will output the value of the variable. The following tags will output the value of the variable "x" returning 5 and the variable "myString" returning "The quick brown fox".
[Var: 'x' = 5]
[Var: 'myString' = 'The quick brown fox']
[Var: 'x'] 5
[Var: 'myString'] The quick brown fox
If the [Var] tag is called with just a variable name and the variable does not yet exist then the variable is created and assigned an initial value of Null. This is sometimes called declaring a variable. The following tag will create the variable "y" and produce no output.
[Var: 'y']
Multiple variables can be created by specifying multiple variable names within the [Var] tag and each variable can be given an initial value. The following tag creates the variable "x" with an initial value of 5 and declares the variable "y" with an initial value of Null.
The initial value of a variable can be the result of an expression or a tag call. The following tags will both create the variable "x" with an initial value of 5.
[Var: 'x' = 5, 'y']
[Var: 'x' = 3 + 2]
[Var: 'x' = (Math_Sqrt: 25)]
The value of a variable can be output using the [Var] tag with a variable name. The following tag outputs the value of the variable "x" returning 5.
The dollar sign $ is a shortcut for the [Var] tag. The $ followed by a variable name outputs the value of a variable. No encoding is applied to the variable value. The $ shortcut is normally used within expressions or within LassoScripts.
$x; 5
$myString; The quick brown fox
[Var: 'x'] 5
Optional encoding keywords can be used with the variable value. Here the variable is first output using the default HTML encoding and then output using strict URL encoding so the spaces are encoded as %20s.
[Var: 'myString'] The quick brown fox
[Var: 'myString', -EncodeStrictURL] The%20quick%20brown%20fox
Case is unimportant when referencing a variable. The expressions $x and $X both refer to the same variable. The tags [Var: 'x'] and [Var: 'X'] will both output the value of the variable "x".
Once a variable has been created it is often necessary to assign new values to it as the page progresses. The [Var] tag can be used for this purpose. If the variable already exists then a subsequent use of the [Var] tag with the same name simply re-assigns the variable value. The following tags result in the variable "x" having the value 10.
The $ shortcut can also be used to assign a new value to a variable. The following expressions again result in the variable "x" having the value 10.
var: 'x' = 5;
$x = 10;
[Var: 'x' = 5]
[Var: 'x' = 10]
Although the $ shortcut can be used to assign a value to a variable, it cannot be used to create a variable. The [Var] tag must be used for that purpose. If you use the [Var] tag to declare variables at the top of your page and then the $ shortcut throughout the remainder of your page then Lasso will flag any variable name typos as syntax errors.
A variable can be set to any expression or tag result. We can set the variable "myString" to the result of a math expression or the result of a math tag. Note that each variable can hold values of any type. Even though the variable "myString" contained a string it can be set to an integer value. Similarly, the variable "x" can be set to a string value.
$myString = 5 * 5;
$myString = (Math_Sqrt: 25);
$x = 'A quick brown fox';
A variable can be set to an expression which uses the same variable as one of its operands. The following script creates a variable "x" with an initial value of 5. The second line sets the variable "x" to the value of the variable "x" plus 5. The third line sets the variable "x" to the value of the variable "x" times 2. Finally, the value of the variable "x" is output resulting in 20.
Var: 'x' = 5;
$x = $x + 5;
$x = $x * 2;
$x; ->20
Again, remember that case is not considered when referencing a variable. Any of the following tags will set the value of the variable "myString" to the specified string value.
[Var: 'myString' = 'A quick brown fox']
[Var: 'MYSTRING' = 'A quick brown fox']
[Var: 'mystring' = 'A quick brown fox']
Lasso has several shortcut operators which make modifying variables in-place easier. The += operator is a shortcut for adding a value to a variable. There are also operators for multiplication *=, division /=, subtraction -= and modulus %=. When using any of these operators, the variable is modified in-place and no output is generated. The script above can be rewritten as follows.
Var: 'x' = 5;
$x += + 5;
$x *= 2;
$x; -> 20
Many member tags also modify a variable in-place and do not output a value. For example. The [String->Uppercase] tag modifies a variable containing a string in-place, making it uppercase, and does not return a value. When the variable is output later you will see that it is now uppercase.
Var: 'myString' = 'A quick brown fox';
$myString->Uppercase;
$myString; -> A QUICK BROWN FOX
Consult the Lasso Reference or the Lasso Language Guide for information about which member tags modify the variable in-place and which return values.
You can check whether a variable has been defined using the [Var_Defined] tag. This tag will tell you whether a variable has been declared at all and will return True even if the variable has a Null value.
[Var_Defined: 'myString'] ->True
You can remove a variable from the current page using [Var_Remove]. This tag undeclares the variable so it is as if it had never been declared. [Var_Defined] will report False for the variable after this tag has been called. It is not normally necessary to call this tag since all variables are destroyed at the end of page processing automatically.
[Var_Remove: 'myString']
You can list all over the variables which are defined on the current page using [Vars->Keys]. When you call this tag you will see the names of several internal variables as well as the names of variables which you have defined.
[Vars->Keys] -> array: (myString), (x), (__result_message__), (__result_code__),
(_at_end), (__error_stack__), (__html_reply__),(__client_postparams__),
(__client_getparams__), (_start), (font), (__triggers__), (__abort__), (__encoding__),
(result), (__http_header__), (__input_encoding__)
The [Vars] tag itself returns the map of all the variables on the page.
The following articles contain more information about some variable related topics.
Variable References
Compound Expressions (Local Variables)
Custom Tags (Local Variables)
Global Variables
More information about all of the tags used in this tip of the week can be found in the Lasso 8.5 Language Guide or in the online Lasso Reference
Author: Fletcher Sandbeck
Created: 7 Oct 2007
Last Modified: 16 Mar 2011
Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.
©LassoSoft Inc 2015 | Web Development by Treefrog Inc | Privacy | Legal terms and Shipping | Contact LassoSoft