Back | Next Lesson

Storing Data Using Variables

Constants & Variables

Constants are literal values, which are (for sake of a better word) constant.  The value can never be changed by the program.  By definition, any number or string enclosed in single quotes is a constant and should be evaluated literally.

Example:

The number one will always be number one, I can do nothing to change that.  The string 'bugs bunny' will always mean "bugs bunny", if I alter this string - it would be a different constant.
Variables, on the other hand, are labels that represents a value and are not to be taken literally.  The value can be dynamic, in that it can be manipulated by your program.

Variables
names have a maximum length of 13 characters and must begin with lowercase alpha character.  After the first character, alphanumeric and "_" characters may be used.  You can not duplicate any Sigmac keyword (level, global, define, if, elseif, else, endif, repeat, until, loop, while, end, call, goto, exit, return, etc.) as a variable.

Tip:    SDI uses "_" as the 1st character for many of their global variable names, so I would stay away from using "_" as the 1st characters- more on globals later in this lesson.

Variable Data Types: Variable declaration:  Sigmac requires that variables must be declared (basically saying "I am an integer and I demand respect") to one of the above data types.  Luckily, Sigmac uses an implicit naming conventions that will automatically declare variables for you.  Quite simple to remember, any variable name that begins with:All variables must be initiated prior to use.  In other words, you need to know how to assign a value to the variable.  This is done with the assignment operator "=" (single equal symbol).  When we first assign a variable a value, it is referred to as initializing.
width = 1.5
The variable is always to the left of the equal symbol and the value (or expression) is always to the right.

Tip:    The compiler interprets the value in database unit at the time when your program is executed (may be feet, inches, meters, etc.- who knows).  You can force the issue by,   width = 1.5"  or width = 1.5cm

Why use variables?  The most obvious answer is to store and manipulate data.  But even more important is flexible code that is maintainable and readable.

Example:
Let's write a program to draw a 2x4 stud on end.  We could use the literal dimensions (1.5" and 3.5") throughout the program.
001     //stud2x4
002     level 1u
003     
004     ::box; =A(0,0,0); =A(1.5",3.5",0);:
005     ::sal; =A(0,0,0); =A(1.5",3.5",0); =A(1.5",0,0); =A(0,3.5",0);:
006     exit
Or we could store these dimensions in variables (width & height) in the beginning of the program and use the variables throughout the program.
001     //stud2x4
002     level 1u
003     
004     width  = 1.5"
005     height = 3.5"
006
007
     plowleft  = A(0,0,0)
008     pupright  = A(width,height,0)
009     pupleft   = A(0,height,0)
010     plowright = A(width,0,0)
011     
012     ::box;=plowleft;=pupright;:
013     ::sal;=plowleft;=pupright;=plowright;=pupleft;:
014     exit
Then when your boss comes and tells you s/he needs a 2x6 program tomorrow, you can change the "height" variable in one place - done!
005     height = 5.5"

Rule:   Never write it twice.
.

String Variables

String variables stores one or more ASCII characters.  You can declare any variable that does not begin with "s" as a string with:
#str bubba
When assigning a ASCII string to a string variable, the ASCII character(s) are enclosed in single quotes (always):
s2 = 'Mary had a '
s3 = 'little lamb'
s4 = ( s2 + s3 )        /* s4 = 'Mary had a little lamb'
Strings are limited to 2048 characters.


Integer Variables

Integer variables stores whole number between -2,147,483,648 and +2,147,483,647.  You can declare any variable that does not begin with "i" as an integer with:
#int count
You can add integer and real variables, but when assigning a real value to an integer variable, rounding is applied accordingly: 
i2 = 1.5                /* i2 is rounded to 1
i3 = 1.6                /* i3 is rounded to 2
i4 = (5 / 2)            /* i4 is rounds 2.5 to 3
Real Variables

Real variables are double precision number between -2,147,483,648.00 and +2,147,483,647.00 with 16 decimal places of accuracy.  You can declare any variable as a real with:
#real px
Point Variables

Point variables stores XYZ coordinates.  You can declare any variable that does not begin with "p" as an absolute point with:
#point origin
You can declare any variable that does not begin with "d" as an relative point with:
#delta origin
Each point variable is comprised of 3 (real number) components, X, Y, and Z.  To access these XYZ components individually:
p2 = (2,3,4)
rx = p2.x               /* rx = 2.0
ry = p2.y               /* ry = 3.0
rz = p2.z               /* rz = 4.0
You can add point variables together:
p2 = (2,2,0)
p3 = (4,4,0)
p4 = (p2+p3)            /* p4 = (6,6,0)
p5 = (p4/2)             /* p5 = (3,3,0)
p6 = ((p2+p3)/2)        /* p6 = (3,3,0)
/* this last formula calcs the midpoint - cool!
When adding point variables with integers or reals, only the X value is modified: 
p2 = (2,2,0)
p5 = ( p2 + 2.0 )       /* p5 = (4,2,0)
When adding point variables with integers or reals, only the X value is modified: 

.

Array Variables

So far the variables we've discuss contain a single value (non-scalar).  An
array (scalar) variable can contain a table of values, similar to a spread sheet.  Arrays allows you to manage likewise data in an orderly fashion.

Arrays follow the same naming and typing rules (integer, real, string, point) as regular variables.  The array's name must be a non-keyword and not previously initialized as a regular variable.

An array can be: Each array consists of a variable
name and subscripts.  Each subscript is a positive integer (including 0) within the "(  )" and separated by commas.  The 3 subscripts formulate an address to a cell, where the data is stored.  You can not mix data types within the array's cells.


Variable's Life Span

Typically a variable (any data type) has a life span for the duration of the executing sigmac.  Once the sigmac terminates, so does its variables.

Introducing
global variablesGlobal variables have a life span of ARRIS itself, only when you exit ARRIS are global variables terminated.  To declare a variable as global:
#global  porigin,  pmin,  pmax  /* Multiple variables are separated by commas.
If you need to declare a variable as global and change its data type:
#global  #int  scg_counter
Therefore global variables can be utilized by any sigmac as long as each sigmac has the above global declaration or you can manage all global variables in a single file to be included at the time of compilation.  More on this concept when we discuss compiling in depth.

Global variables can either be an array (scalar) or non-scalar variables.  Global variables follow the same naming rules as regular variables.

You can verify that a global variable is not used by another application by echoing it in ARRIS with the application loaded.
!scg_counter
.

More than you want to know:

There are system contants (e.g. #true = 1, #false = 0, #vinf = 2147483647, etc.).  The value of a system constant can be obtained by echoing it in ARRIS:
 
!#vinf                  /* largest possible number in ARRIS
 
There are system variables (e.g. #fcol = current color setting in ARRIS or #fpen = current pen setting in ARRIS).  The value of a system variable can be obtained by using $getvar() or $getflg() and echoing it in ARRIS:
 
!$getvar(#vlastkey)     /* returns the ascii value for the last key entered
 
or
 
!$getflg(#flin)         /* returns the current line type
There is not a good way to distinguish a system #variable from a system #constant, they both are prefixed with #.  Most #variables beginning with "#f" are object (entity variables) flags.

System global variables are not to be confused with "system #variables".  System #variables are declared in the private C-level of ARRIS, while the system global variables are declared in the public sigmac level.  SDI manages their global variables in files ending in "__glob.ff" in their source code directories.


That's all for now,
Steve

I would like to thank Jeff Small for his addition on lesson #003, using a "make" file in UNIX and for pointing out that for every time you compile your sigmac - you need to reload the sigmac library in ARRIS for your changes to take affect.  I welcome all the help I can get.

This lesson was brought to you by SCG consulting. All written materials related to these Sigmac lessons are copyrighted by SCG and intended for personal use only. Any commercial or non-commercial reproduction for public use is prohibited without written consent from SCG.

Back | Next Lesson