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.5: SynonymsSynonyms are used by Gri to store character strings. Gri denotes synonyms with words beginning with backslash (e.g., `\syn '),
following the TeX convention.
10.5.1: Naming convention for synonymsSynonym names begin with a backslash (e.g., `\filename '). After
the backslash, Gri expects a letter (upper or lower case) or one or more
periods. Following this is an arbitrary string of letters, numbers, or
underscores. If there are periods at the start, then the same number of
periods must be used at the end. The following are some examples
Gri defines several synonyms for its own use, so that if you modify these, you may get strange results. Each of these starts and ends with a single period. There is an exception to the above rule, one which mostly comes up when using netCDF files which may have variable names that may contain punctuation. Gri permits synonym names to have punctuation characters (but not blanks or tabs) in synonym names, provided that the second character in the name is an opening brace and that the last character is a closing brace, e.g.
This is used particularly for files in the netCDF format,
for reading variable attributes, which by netCDF convention use a colon
(`
`
Synonyms may be freely embedded in strings (a common example is
`
To get a backslash in a string without Gri thinking it is part of a
synonym, use two backslashes (e.g.,
`
Similarly, if your system command is expecting to see `
The ` 10.5.2: Some uses for synonymsSynonyms store strings and are useful for anything strings are useful for, e.g. filenames, plot labels, names of variables, etc.
10.5.2.1: Using synonyms to generalize codeSynonyms are often used to store filenames, since then only a single line of a file may need to be altered, in order to work with another file, e.g.
10.5.2.2: Using synonyms to store OS outputSynonyms provided a convenient way to store information from the OS.
This example uses the Unix system commands `
NOTE: As usual, if the system command contains the Gri comment
designator (the string ` 10.5.2.3: Storing user responses via `
You can ask the user for the contents of strings:
|
query \filename "What's the data file?" ("file.dat") |
The prompt `What's the name of the data file?
' is typed to the
terminal, and whatever string the user types is inserted into the
synonym `\filename
'. If the user types nothing, but simply presses
carriage return, the (optional) default string (which must be enclosed
in parentheses as shown) is put into `\filename
'. Note that the
default is ignored if it is not written properly: it must be enclosed in
double quotes enclosed in parentheses, with no intervening spaces.
open \directory_file read \file_name close open \file_name read columns x y |
The first (space-separated) word is read into the the first synonym after
the `read
' command, the second word into the second synonym, and so on.
If the word you want is not near the front of the line, you can use the command
`read line
' to get the whole line, then use the method described above
to extract the word you want. Indexing begins with 0, remember.
word of
' syntax.
\sentence = "This sentence has five words" \first_word = word 0 of "\sentence" \last_word = word 4 of "This sentence has five words" |
[]
' syntax
The number in the square brackets may be a constant, a variable, or a synonym, but not a more complicated expression. If the index value is a floating-point number, it is first rounded to the nearest integer. If the index value is negative or exceeds the number of words minus 1, then an empty string is retrieved.
If no number appears in the square brackets, the result is the number of words in a synonym.
\syn = "This has 4 words in it" show "\[0]syn ... gives 'This'" show "\[1]syn ... gives 'has'" .i. = 3 show \[.i.]syn ... gives 'words'" \i = "3" show \[\i]syn ... gives 'words'" show "\[]syn ... gives '6', i.e. number of words" |
Global synonyms are shared among commands. To see the built-in global
synonyms (see Index of Builtins) use `show synonyms
', which
produces output that looks something like the following.
Synonyms... \.missingvalue. = "10000000000000000000000.000000" \.return_value. = "" \.version. = "2.7.0" \.pid. = "3043" \.wd. = "/home/kelley" \.time. = "Sun May 20 13:18:32 2001" \.user. = "kelley" \.host. = "Intrusion.phys.ocean.dal.ca" \.system. = "unix" \.home. = "/home/kelley" \.lib_dir. = "/usr/share/gri" \.command_file. = "stdin" \.readfrom_file. = "stdin" \.ps_file. = "gri-00.ps" \.path_data. = "." \.path_commands. = "." |
These things will be obvious to unix users; for example
`\.pid.
' is the process ID of the job (often used in names for
temporary files), and `\.wd.
' is the working directory (often used
in `draw title
' commands to indicate in which directory the gri job
was run.
Some commands set `\.return_value.
' to non-blank; the
meaning of the return value varies from command to command.
\@alias
' syntaxGri handles this by so-called "alias" synonyms, which store the names of other items.
The syntax is simple. Suppose that a synonym, called `\pointer
'
say, contains the name of another synonym, or a variable. Then
you may use `\@pointer
' anyplace you would normally use the item
named.
.pi. = 3.14 \pi_pointer = ".pi." show \@pi_pointer # just like 'show .pi.' |
# Print approximation to 2*Pi .pi. = 3.14 \pi_pointer = ".pi." \@pi_pointer *= 2 show .pi. |
show synonyms
', but they can be used freely in commands like
`show "Number of words is \.words."
'.
\.words.
'. The RPN operator `wordc
'
also yields the same value (see Solitary Operators).
\.word0.
', the second
`\.word1.
', etc. (Note that this is the C convention, not the
FORTRAN convention. If `\.words.
' is 2, then `\.word0.
' and
`\.word1.
' are defined, but `\.word2.
', which FORTRAN programmers
expect, will not be defined.) If you don't know the place of the synonym
in advance (i.e. 0 versus 1, for `\.word0.
' versus `\.word1.
'),
then use the RPN operator `wordv
' instead (see Unary Operators).
\.proper_usage.
'. This is useful in tests of syntax
(see Adding New Commands). For example:
`draw depths from \file' Draw depth data stored in indicated file. If the filename contains periods or slashes, you'll have to enclose it in double quotes, as in the second example: draw depths from file upper_cove draw depths from file ../old_data/upper_cove { if {rpn \.words. 4 !=} show "FATAL ERROR in `\.proper_usage.':" show " Need 4 words; got \.words. words." quit end if # Right number of words, so continue onward... } |
New Thing [option]
'. If you call
it with `New Thing
', then (within `New Thing
') `\.words.
'
will be `"2"
', `\.word0.
' will be `"New"
' and `\.word1.
'
will be `"Thing"
'. On the other hand, if you call it with
`New Thing 22.3
' then `\.words.
' will be `3
',
`\.word0.
' will be `"New"
', `\.word1.
' will be
`"Thing"
' as before, and `\.word2.
' will be `"22.3"
'.
EXAMPLE Here is a new command to label lines drawn by
`draw curve
':
`Draw Label For Last Curve "label"' Draw a label for the last curve drawn, using ..xlast.. and ..ylast.. built-in variables. { new .draw_label_for_last_curve_graylevel. .draw_label_for_last_curve_graylevel. = ..graylevel.. set graylevel 0 draw label "\.word5." at \ {rpn ..xlast.. xusertocm 0.1 + xcmtouser} \ {rpn ..ylast.. yusertocm \ ..fontsize.. pttocm 2 / - ycmtouser} set graylevel .draw_label_for_last_curve_graylevel. delete .draw_label_for_last_curve_graylevel. } open file.dat read columns x y draw curve \label = "Illustration" Draw Label For Last Curve "\label" |
(Note that Gri has a built-in command
`draw label for last curve "\label"
' written much as above, so
there is no need for you to enter
this new command into your `.grirc' file. But you might want to
check `gri.cmd' to see how a full
command does checking of the calling syntax
(see Invoking Gri).