Chapters:
1: Introduction
2: Simple example
3: Invocation
4: Finer Control
5: X-Y Plots
6: Contour Plots
7: Image Plots
8: Examples
9: Gri Commands
10: Programming
11: Environment
12: Emacs Mode
13: History
14: Installation
15: Gri Bugs
16: Test Suite
17: Gri in Press
18: Acknowledgments
19: License
Indices:
Concepts
Commands
Variables
|
10.4: Variables
10.4.1: About variables
Variables store numbers. As it reads your program, Gri
substitutes variable values any place a variable appears where a number
normally would. For example, in the code below `.number. ' is a
variable storing the value 10, so the two `read ' statements have
the same effect:
.number. = 10
read columns .number. x y
read columns 10 x y
|
Variable names begin and end with a single period (example:
`.num. '). (Gri uses this odd notation to distinguish variable
names from ``normal'' words, which is necessary because Gri does not
have a limited list of keywords as other languages do. Thus, the C
programming language is happy to let you use a variable name like
`latitude ', since it is not a keyword, but Gri is not, since it
might like to use that word itself in a new command.)
You should not use names beginning and ending with double periods,
because Gri uses names like that to store built-in variables for its own
use (e.g., `..xsize.. ' saves the width of the plot).
To store a number into a variable, use a command like
or
Storage is automatically set aside when you assign into a nonexistent
variable; no ``declaration'' statements are required as in the C
language.
The Gri command, `new ' (see New), allows you to have several
``versions'' of a variable. This is useful for local storage in new
commands, inside `if ' statements, etc, since it lets you use
temporary variables without worrying about overwriting values outside
the local block of code. The syntax is `new .variable. = value '
(where, as usual, `value ' may be an rpn expression
(see rpn Mathematics). Here is an example:
`foo bar'
{
new .a. # Get storage
.a. = 10 # Store a local value
show "Locally, .a.=" .a. " (expect 10)"
delete .a. # Delete this local one
}
.a. = 1
show "Global version has .a.=" .a. " (expect 1)"
foo bar
|
To see if a given named variable (or synonym) exists, use the RPN
operator `defined ' (see rpn Mathematics).
10.4.2: User variables
You can get Gri to read values for variables from your file. Here's how
to read a number from a header line and then read that many lines of
columnar data:
open file.dat
read .num.
read columns .num. x y
|
You can define variables within the Gri program:
.num. = 10
read columns .num. x y
|
You can get variables interactively from the user, using the
`query ' command. (If the user types carriage-return, or if the
command-line flag `-y ' was specified when invoking Gri, the value
100 will be assigned to `.num. ').
For example,
query .num. "Number of rows to read?" (100)
read columns .num. x y
|
Gri allows you to use a previous value of the variable in the default
string, as in this example:
.start. = 8 # default
.stop. = 2 # default
query .start. "Start time? " (.start.)
query .stop. "Stop time? " (.stop.)
|
Variables can be manipulated using reverse polish notation (RPN)
mathematical operations (see rpn Mathematics).
Variables are often useful in `if ' statements. Here are some
examples:
read .num_pts.
if .num_pts.
show "There are some data"
read columns .num_pts. x y
else
show "There are no data"
end if
# ...
read .latitude.
if {rpn .latitude. 10 <}
read .num.
read .num. x y
draw curve
else
show "Skipping data North of 10deg N"
read .num.
skip .num.
end if
|
10.4.3: Built-in variables
Built-in variables (see Index of Builtins) have names which begin and
end with two periods. For example, `..xsize.. ' is the width
of the x-axis in centimetres. You may use these variables as you wish
(example: `..xsize.. = 4 ' is an alternative to
`set x size 4 '), but you must be aware that these are not ``free''
variables for you to use for arbitrary purposes. You can find out what
the built-in variables are by the command `show variables '.
There are two types of variables
-
Startup variables, which are created by Gri at startup time.
These variables can be relied upon to exist (barring changes in Gri
itself), unless you `
delete ' them.
-
Spontaneous variables (which are created by certain Gri commands,
and only exist if these commands have been executed). For example, the
`
regress ' command defines `..coeff0.. ' (the intercept of the
fitted line), `..coeff1.. ' (the slope of the fitted line),
`..R2.. ' (the correlation coefficient).
To see the values of the built-in variables (along with the user
variables), use `show variables '. Here are some useful builtin
variables:
You may use any of these built-in variables anywhere. For example,
here's how to stack 3 graphs vertically on the page:
.offset. = {rpn ..ysize.. 3 + }
open file1
read columns x y
close
draw axes
draw curve
..ymargin.. += .offset.
open file2
read columns x y
draw axes
draw curve
close
..ymargin.. += .offset.
open file3
read columns x y
draw axes
draw curve
close
|
The first line needs a bit of explanation. It is a reverse-polish
expression. The format is `{ ' followed by `rpn ' followed by
an expression followed by `} '. Within the expression, spaces must
separate operands. This makes `.offset. ' equal to the height of
y-axis plus 3 cm, so plots are separated by 3 cm. To learn more about
`{rpn ... } ' (see rpn Mathematics).
Another possibly unfamiliar thing is the code `+= '. It means take
the thing on the left hand side, and add to it the thing on the right
hand side. (In this case, it is used to increase the y margin by the
value of `.offset. '.)
|