VARIABLES.DOC

advertisement
3.2.
Creating Variables
In GENESIS, you create a variable by entering a variable ``type''
specifier at the command line, followed by the variable name (a script
identifier), optionally followed by a value to assign to the variable:
variable-type variable-name [= expression]
You can create any of three types of variables at the GENESIS shell:
----------------------------------------------------------------------Type
Meaning
Example Value
----------------------------------------------------------------------int
integer number
5, 18, 6000, -34
float
double precision floating-point number
5.0, 3.14, -2001.4
str
character string
five, 5.0, hello
----------------------------------------------------------------------Here are some examples of correct variable declarations:
genesis
genesis
genesis
genesis
genesis
>
>
>
>
>
int a
float PI = 3.141593
float myfloat = 2*PI
float floatstr = "6.3"
str hi = "hello there"
Note that case is significant for variable names (as in other areas):
``PI'' is different from ``Pi'', which is different from ``pi''. Also
note that the character sequence ``6.3'' could be the value of either
a floating point variable or a string variable.
Once you have defined a variable, you can change its value using an
assignment statement:
variable-name = expression
For example:
genesis > a = 40
genesis > myfloat = 100.5
genesis > hi = "The value of myfloat = " @ myfloat
3.2.1.
Local and Global Variables
Variables declared in a function are local to the function. Those
declared outside of a function in a script (or script which is
included with the ``include'' statement) are global variables. When a
value is assigned to a global variable, it will affect any statements
or functions which follow this assignment. One often refers to
``constants'' in a GENESIS script. These are not true constants, but
are just variables which are not expected to take on new values during
the course of a simulation.
3.2.2.
Using Variables (The echo Command)
Script variable values are retrieved by naming the variable in the
context of an expression. In the description of GENESIS script
language statements, expressions appear as indicated. In other
contexts the value of an expression may be subtituted by enclosing the
expression in curly brackets:
genesis > float pi = 3.1415
genesis > echo pi
pi
genesis > echo { pi }
3.1415
As with many languages, GENESIS will cast expressions involving both
floating point and integer variables to floating point. It will also
convert a string to a float if the string is a valid representation of
a number. For example,
genesis > int i = 5
genesis > int j = 2
genesis > float x = 2
genesis > str num = "5"
genesis > echo {i/j}
2
genesis > echo {i/x}
2.5
genesis > echo {num/2}
2.5
genesis > num = "i/x"
genesis > echo {num}
i/x
genesis > echo {{num}/2}
** Error - CastToFloat: Error casting 'i/x', using 0.0
0
Download