In most cases, this is what you would intuitively expect to happen.
If you use the Literal or Evaluate option, then the option must be placed immediately after the Parse command. Literal and Evaluate are mutually exclusive options.
Example:
Global hello `Hello Peter !` Parse Literal Value hello . name . Say "Person greeted: {name}"
Here, we have to use Literal since the string can not be evaluated: for that, "Hello" would have to be a function, "Peter" an argument hence a Global variable, and the exclamation mark would have to have some algebraic meaning, which it doesn't.
[This is default.]
If you use the Literal or Evaluate option, then the option must be placed immediately after the Parse command. Literal and Evaluate are mutually exclusive options.
Example:
Say "Open a metafile (yes/no) ?" Global yes True Global no False Global y True Global n False Parse Evaluate Terminal metafile If metafile Then Say "Opening metafile {input}.ps ..." !add meta type PostScript file-name "{input}.ps" !open meta !act meta Else Say "No metafile opened." Endif
The If statement expects a Logical as condition. We would however like to offer the user the possibility to reply with "yes" or "no". Therefore, we define Global variables by those names which have the values True and False. With the Evaluate option, a reply of "yes" therefore results in the assignment of Logical value True to the global variable METAFILE. With the Literal option, METAFILE would become a String with value `YES`.
[This is not default.]
Examples:
Garfield is started by issuing a command like:
garfield -arg "Hello !"The double quotes around the argument string are needed only in case of special characters, such as the exclamation mark. Once the program is running, you can access the string "Hello !" with the Parse command:
Parse Arg greeting Say {greeting}
Imagine you've an input file called factorial which looks like this:
Parse Arg n If type(n)#`Number` Then Say "Please supply a numeric argument." Elseif n<1 | abs(entier(n+0.5)-n)>1e-4 Then Say "Please supply a strictly positive, integer argument." Else Global fac=1 For i From 2 To n Do Global fac=fac*i Enddo Say "Factorial of {n} is {fac}." EndifOne would run this file as follows:
<factorial 5
Parsing should not give surprising results with Globals that are of type Number or String. Globals of type Logical will result in the string `True` or `False` while variables of type Undefined yield the string `Nill`.
If the Global variable is an Histogram, then the resulting string will be `Histogram`, which is not useful.
Similarly, a Matrix with more than 1 dimension gives the string `n-Matrix` where n is the number of dimensions. Only for 1-dimensional matrices will the string contain a formatted portion of the matrix. This can be parsed, although not trivially:
Call book_matrix(a,4) Parse Global a `(` a `,` b `,` c `,` d `)`
which is equivalent to:
Parse Value row(4) `(` a `,` b `,` c `,` d `)`
This format is only offered for completeness - the Terminal input format is probably more useful. See there for further details.
The input line is treated like all other input, i.e. text outside single and reverse quotes is converted to upper case.
This is a frequently used format since it permits obtaining user input while a file is being input.
This format can also be used to pause execution of an input file, while waiting for the user to hit the return key:
&FIELD grid 5 print ex,ey Parse Terminal dummy print v
The first table will be printed, then the program waits for a response from the user and the 2nd table is printed only after the return key has been pressed.
This format is similar to Parse Global, see there for examples and further comments.
A template can contain the following elements:
Element | Purpose |
---|---|
`string` |
Locate string in the input, resume from there |
var |
Global variable to which the field is assigned |
. |
Ignore the corresponding input field. |
All elements can be repeated many times.
Instead of reverse quotes (`), one can also use single and double quotes to delimit search strings. Keep in mind however that the quote is a syntax element - the quote must therefore still be visible after the Parse statement has been processed by the usual input routines.
If there are more variables in the template than elements in the input, then the variables to which no value can be assigned will be set to have type Undefined. Conversely, if there are more elements than variables, then all variables will together, with their separators, be assigned to the last variable. This operation is likely to fail in the Evaluate mode.
Examples with option Literal:
Input | Template | Result |
---|---|---|
1 2 3 |
x y z |
x=1, y=2, z=3 |
1 2 x+1 |
x y z |
x=1, y=2, z=`x+1` |
1 2 3 4 |
x y . |
x=1, y=2 |
1 2 |
x y z |
x=1, y=2, z=Nill |
1 2 3 |
x y |
x=1, y=`2 3` |
1 2 A 3 |
x . `A` y |
x=1, y=3 |
1 2 A 3 |
x `A` y |
x=`1 2`, y=3 |
1 `abc` 2 |
x y . |
x=1, y=`abc` |
1>2 |
z . |
z=`1>2` |
Examples with option Evaluate:
Input | Template | Result |
---|---|---|
1 2 3 |
x y z |
x=1, y=2, z=3 |
1 2 x+1 |
x y z |
x=1, y=2, z=2 |
1 2 3 4 |
x y . |
x=1, y=2 |
1 2 |
x y z |
x=1, y=2, z=Nill |
1 2 3 |
x y |
Error: y := 2 3 is incorrect syntax |
1 2 A 3 |
x . `A` y |
x=1, y=3 |
1 2 A 3 |
x `A` y |
Error: x := 1 2 is incorrect syntax |
1 `abc` 2 |
x y . |
x=1, y=`abc` |
1>2 |
z . |
z=False |
Formatted on 21/01/18 at 16:55.