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

Indices:
Concepts
Commands
Variables

## 10.5: Synonyms

Synonyms 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 synonyms

Synonym 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
  \simple = "Howdie" \.longer_example. = "Dots and underscores are ok too" \a2 = "OK for number at end ..." \a3bb = "... and inside" 

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.

  \{foo.bar} = "Foo bar" 

This is used particularly for files in the netCDF format, for reading variable attributes, which by netCDF convention use a colon (:') to separate variable name and attribute name (see Read Synonym or Variable). For more information on netCDF format, see

http://www.unidata.ucar.edu/packages/netcdf/index.html' here .

Synonyms may be freely embedded in strings (a common example is draw title "Data from file \datafile'"'. They may also appear anywhere in commands (e.g., open \filename'). The exception to this rule is that Gri ignores your synonyms within math mode, in order to prevent clashes (e.g. you might define \alpha' as a synonym storing the value "foo bar"', but Gri will ignore this within math-mode, so that $\alpha$' will still mean the Greek letter alpha).

To get a backslash in a string without Gri thinking it is part of a synonym, use two backslashes (e.g., show "The backslash character \\ is used for synonyms."'). This may sometimes be required in system' commands (see System), to prevent Gri from converting substrings like \n' (which many system commands use to represent the newline character). For example, the command system perl -e 'print "foo\nbar";'' will be mangled if Gri has already been told that \nbar' is a synonym. (There will be no problem if \nbar' is not an existing synonym, since Gri will then just leave it in place.) To be sure that no mangling can occur, replace each backslash with two backslashes. This tells Gri not to try to substitute a synonym at that location. In the example below, the first system call prints fooled you!' on one line line, because Gri substituted for what it thought was a synonym called \nbar'; the second (correctly) prints foo' on one line and bar' on the next.

  \nbar = "led you!" system perl -e 'print "foo\nbar\n";' system perl -e 'print "foo\\nbar\\n";' 

Similarly, if your system command is expecting to see \t' (for a tab character), then you must write \\t' to prevent Gri from trying to substitute a synonym named \t'.

The show' command has a special syntax for permitting newlines and tabs in strings (see Show).

### 10.5.2: Some uses for synonyms

Synonyms 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 code

Synonyms 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.

  \filename = "columns.dat" open \filename # a lot more code using the file name 

#### 10.5.2.2: Using synonyms to store OS output

Synonyms provided a convenient way to store information from the OS.

  # Show the date. \date = system date show "Time is \date" # Show the command file name, then use the system # to construct a filename with the same beginning # but ".dat" as the ending instead of ".gri". show "The commandfile name is \.command_file." \fn = system echo basename \.command_file. .gri.dat show "A filename constructed from this is \fn" 

This example uses the Unix system commands echo' and basename' to construct a filename ending in .dat', from the command file name (stored in the builtin string \.command_file.'), assuming that the command file name ends in .gri'.

NOTE: As usual, if the system command contains the Gri comment designator (the string #'), protect it with double-quotes (see System).

#### 10.5.2.3: Storing user responses via query'

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.

#### 10.5.2.4: Storing File Contents

You can read the contents of synonyms from a file:

  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.

#### 10.5.2.5: Working with words within strings

Sometimes a synonym will contain several words that you need to work with indidually (e.g. it might contain a list of files that should be processed). There are two ways to do this.

The 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" 

The []' syntax
Individual words of synonyms may be accessed by prefixing the synonym name with the index number of the word (starting at 0) enclosed in square brackets.

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" 

### 10.5.3: Some important builtin synonyms

Within mathematics mode (portions of strings enclosed within dollar-signs), Gri stores the definitions of many Greek letters and mathematical symbols as math-mode synonyms (see Mathematical Text).

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.

### 10.5.4: Alias synonyms: the \@alias' syntax

Sometimes you need to work with a variable or a synonym whose name can only be determined at run-time, perhaps through interaction with the user, examination of a datafile, or examination of the command provided to the OS when invoking Gri.

Gri 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.

Illustrations of using the value of a named item
The following prints an approximation to Pi followed by the name of movie star.
  .pi. = 3.14 \pi_pointer = ".pi." show \@pi_pointer # just like 'show .pi.' \hero = "Gregory Peck" \our_hero = "\\hero" show "\@our_hero" # just like 'show "\hero"' 

Illustrations of assigning to a named item
The following prints an approximation to 2*Pi and yet another star; the point is that the alias appears to the left of an assignment operator.
  # Print approximation to 2*Pi .pi. = 3.14 \pi_pointer = ".pi." \@pi_pointer *= 2 show .pi. # Stars don't shine alone \hero = "Gregory Peck" \our_hero = "\\hero" \@our_hero = "Harrison Ford" show "\hero" 

### 10.5.5: Local synonyms

Local synonyms are created by Gri upon entry to a Gri command. You use them to parse the command line that was used in calling the new command, to look for options, gather filenames, etc. Local synonyms are known only from within the local Gri command. They are not listed by show synonyms', but they can be used freely in commands like show "Number of words is \.words."'.

• Within any new Gri command, the number of words in the line that called the command is available in \.words.'. The RPN operator wordc' also yields the same value (see Solitary Operators).
• The first word in the calling line is \.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).
• Within any new Gri command, the proper calling usage is available in \.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... } 

These synonyms help you scan for optional words in commands. Suppose you have defined a new command 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).