Simple templates for parsing into words
Parsing into words removes leading and trailing blanks from each word before it is
assigned to a variable. The exception to this is the word or group of words assigned
to the last variable. The last variable in a template receives leftover data, preserving
extra leading and trailing blanks. Here is an example:
-
/* Preserving extra blanks */
-
solar5=’Mercury Venus Earth Mars Jupiter ’
-
parse var solar5 var1 var2 var3 var4
-
/* var1 =’Mercury’ */
-
/* var2 =’Venus’ */
-
/* var3 =’Earth’ */
-
/* var4 =’ Mars Jupiter ’ */
解释一下:
默认的template的 delimitor是空格,被blank-delimitorparse的 substring 在赋值给var是,除了除了最后一个是直接赋值给变量,其它substring的leading 以及trailing blanks 都会被删除掉。
The period as a placeholder
A period in a template is a placeholder. It is used instead of a variable name, but it
receives no data. It is useful:
v As a “dummy variable” in a list of variables
v Or to collect unwanted information at the end of a string.
-
/* Period as a placeholder */
-
stars=’Arcturus Betelgeuse Sirius Rigil’
-
parse var stars . . brightest . /* brightest=’Sirius’ */
-
-
/* Alternative to period as placeholder */
-
stars=’Arcturus Betelgeuse Sirius Rigil’
-
parse var stars drop junk brightest rest /* brightest=’Sirius’ */
A placeholder saves the overhead of unneeded variables.
简单template以及包含string template 的parsing:
Here are two templates: a simple template and a template containing a literal string
pattern:
var1 var2 /* simple template */
var1 ’, ’ var2 /* template with literal string pattern */
The literal string pattern is: ’, ’. This template:
解析过程:
v Puts characters from the start of the source string up to (but not including) the
first character of the match (the comma) into var1
v Puts characters starting with the character after the last character of the match
(the character after the blank that follows the comma) and ending with the end of
the string into var2.
First, the language processor scans the source string for ', '.
It splits the source
string at that point. The variable ln receives data starting with the first character of
the source string and ending with the last character before the match. The variable
fn receives data starting with the first character after the match and ending with the
end of string.
A template with a string pattern omits data in the source string that matches the
pattern.
A template with a string pattern can omit some of the data in a source string when
assigning data into variables. The next two examples contrast simple templates with
templates containing literal string patterns.
-
/* Simple template */
-
name=’Smith, John’
-
parse var name ln fn /* Assigns: ln=’Smith,’ */
-
/* fn=’John’ */
Notice that the comma remains (the variable ln contains ’Smith,’). In the next
example the template is ln ’, ’ fn. This removes the comma.
-
/* Template with literal string pattern */
-
name=’Smith, John’
-
parse var name ln ’, ’ fn /* Assigns: ln=’Smith’ */
-
/* fn=’John’ */
无法匹配的情况:
If the source string does not contain a match for a string pattern, then any variables
preceding the unmatched string pattern get all the data in question. Any variables
after that pattern receive the null string.
A null string is never found. It always matches the end of the source string.
Parsing multiple strings
Only ARG and PARSE ARG can have more than one source string. To parse
multiple strings, you can specify multiple comma-separated templates. Here is an
example:
parse arg template1, template2, template3
This instruction consists of the keywords PARSE ARG and three comma-separated
templates. (For an ARG instruction, the source strings to parse come from
arguments you specify when you call a program or CALL a subroutine or function.)
Each comma is an instruction to the parser to move on to the next string.
Example:
-
/* Parsing multiple strings in a subroutine */
-
num=’3’
-
musketeers="Porthos Athos Aramis D’Artagnon"
-
CALL Sub num,musketeers /* Passes num and musketeers to sub */
-
SAY total; say fourth /* Displays: "4" and " D’Artagnon" */
-
EXIT
-
-
Sub:
-
parse arg subtotal, . . . fourth
-
total=subtotal+1
-
RETURN
Note that when a REXX program is started as a command, only one argument
string is recognized. You can pass multiple argument strings for parsing:
v When one REXX program calls another REXX program with the CALL instruction
or a function call.
v When programs written in other languages start a REXX program.
If there are more templates than source strings, each variable in a leftover template
receives a null string. If there are more source strings than templates, the language
processor ignores leftover source strings. If a template is empty (two commas in a
row) or contains no variable names, parsing proceeds to the next template and
source string.
REF:TSO/E REXX Reference
阅读(1882) | 评论(0) | 转发(0) |