BACK
The PARSE Editor
PARSE VAR InVar Out1Var Out2Var Out3Var
where: InVar = String to be split
Out1Var = 1st word of InVar
Out2Var = 2nd word of InVar
Out3Var = All the Rest of InVar
PARSE VAR InVar Out1Var . Out2Var
where: InVar = String to be split
Out1Var = 1st word of InVar
. = Loose 2nd Word
Out3Var = All the Rest of InVar
How PARSING works!
If you ask REXX programmer to explain PARSING they normally tell you to
come back when you have 3 weeks to spare, as a result people get frightened
of it all. I have to admit it's more involved than other areas, but it's
not really more complex. In these
examples you can see we are splitting up the contents of one variable
'InVar' and putting the bits in other variables. Note that the contents
of this first variable ('InVar') is not changes. In the first template
'Out1Var' and 'Out2Var' take the first and second words of 'InVar', The
last variable gets the remainder of 'InVar'. If we want to 'loose'
a word just put in a full stop instead of a variable name.
This is what we have done in the second template.
PARSE VAR InVar Out1Var "," Out2Var
where: InVar = The Input Variable
Out1Var = Gets everything before first comma
"," = the pattern marker
Out2Var = Gets everything after first comma
PARSE VAR InVar Out1Var (In2Var) Out2Var
where: InVar = Contains the string to be split
In2Var = contains the pattern marker
Out1Var = gets everything before pattern marker
Out2Var = gets everything after pattern marker
Pattern Markers
In the last bit a string was split on spaces.
this is the default pattern marker, here in the first template we have
used a comma instead. So if we code a string between two variables
then we split at the point where we find a match for the string.
This string can be of any length. In the second tenplate
the string we want to split at is coded in a vartiable, so
somehow we have to tell the PARSE editor that the variable
contains a pattern marker and is not an output variable.
As you can see this is achieved by putting the variable in brackets.
If A = ' PARSE can split up words.'
After: 'PARSE VAR A B C D'
B = 'PARSE' C = 'can'
D = ' split up words.'
If A = ' Use a fullstop as a dummy variable.'
After: 'PARSE VAR A B . C '
B = 'Use'
C = ' fullstop as a dummy variable.'
If A = 'can split at pattern markers.'
After: 'PARSE VAR A B 'at' C'
B = 'can split ' C = ' pattern markers.'
If A = 'The "pattern marker" now in a variable.' and PatMark= '"'
After: 'PARSE VAR A B (PatMark) C (PatMark) D'
B = 'The ' C = 'pattern marker'
D = ' now in a variable.'
PARSE Examples
Here are some PARSE examples. Note that leading and trailing spaces
are being stripped off. It also might be worth saying here
that there may be very slight differences on stripping on different
platforms, do a few tests to find out.
PARSE VAR InVar Out1Var 23 Out2Var
where: InVar = Input variable
Out1Var = gets columns 1 -22 of InVar
23 = Split Position
Out2Var = gets 23 to end of InVar
PAR VAR InVar . ":" Out1Var +8 .
where: InVar = input variable
. = loose everything to the first colon
":" = pattern marker
Out1Var = starts ":" for 8 characters
+8 = length of previous varible (Out1Var)
. = dump the rest
Numerical Pattern Markers
Now the pattern markers are numbers and not strings. In the first
template we can see that coding a number indicates a split position.
The previous variable (Out1Var) gets everything up to this position and
the next variable (Out2Var) starts at this point. The
second template shows that when a number has a plus sign it means
that the number is the length of the previous variable.
In this 2nd template the result would be the same if the second full stop
was not coded.
PARSE VAR InVar Out1Var =(In2Var) Out2Var 20 .
where: InVar = Variable containing a string
Out1Var = Gets the first bit of InVar
=(In2Var) = The contents of In2Var is the split point
Out2Var = Get 20 characters from passed split point
20 = The length of Out2Var
. = Dump the rest
PARSE VAR InVar Out1Var +(InVar2) .
where: InVar = variable containing a string
Out1Var = firstr bit of InVar
+(InVar) = the contents of In2Var is the length of Out1Var
. = dump the rest
Numerical Position Pattern Markers in Variables.
In both these templates numerical values are not defined as constants
but in variables. In the first the equal sign and the brackets mean the
contents of 'In2' determining the split point. Its value must be a zero
or a positive integer
number . In the second template the plus sign means that the
value of the variable is
the length of the previous variable (here 'Out2' ).
For all of these 'A' = '123456789abcdefghij'
After: 'PARSE VAR A 3 B 8 C'
A = '12'
B = '34567'
C = '89abcdefghij'
After: 'PARSE VAR A 3 B +8 C'
A = '12'
B = '3456789a'
C = 'bcdefghij'
If Pos= '4'
and Len= '6'
After: 'PARSE VAR A =(Pos) B +(Len) C'
A = '123'
B = '456789'
C = 'abcdefghij'
PARSE Examples - 2
Here are some more PARSE examples showing positional pattern markers.
No stripped is done on these of course. You can mix positional and
literal pattern markers.
PARSE VAR InVar Out1Var Out2Var =(Out1Var) .
where: InVar = variable containing a string
Out1Var = the first word of Invar
Out2Var = the next part og InVar
=(Out1Var) = Out1Var holds the length of Out2Var
. = dump the rest
PARSE VAR InVar Out1Var 15 Out2Var 1 Out3Var
where: InVar = variable containing a string
Out1Var = gets the first 14 chars. of InVar
15 = split position
Out2Var = gets 15 to end of InVar
1 = split position
Out3Var = whole of inVar
Complex PARSE instructions
Life's getting slightly more complex here. In the first template the value
of the positional pattern variable is extracted from the input string just
before its used to determine the length of the next variable! The second
template details what happens when a numeric
positional pattern has a value less than a previous positional
pattern - it just goes back to that position. In the example here
we go back to the 1st character so 'Out3Var' would be the same as 'InVar'.
PARSE VALUE "a long string" WITH Out1Var Out2Var etc
where: "a long string" = the input to PARSE
Out1Var etc = output variables and pattern markers
PARSE VALUE funct() WITH Out1Var etc
where: funct() = function result
Out1Var etc = output variables and pattern markers
PARSE using VALUE & WITH
In the last templates the input to the editor could only be a variable.
Using VALUE and WITH the input can be just about anything.
In the first template here we are splitting up a literal
string constant that has been coded. To be honest you are unlikely
to want to do this in real life, however you will want
to do what is shown in the second template. Here we are
splitting up the return from a function. Anything after the
word 'WITH' are either the output variables or pattern markers.
This produces a very powerful feature.
/* REXX. PARSE Editor continued */
SAY "If TIME() = '" || TIME() || "'"
SAY ' After: ''PARSE VALUE TIME() WITH hh ":" mm ":" ss'''
PARSE VALUE TIME() WITH hh ":" mm ":" ss
SAY " hh = '" || hh || "'"
SAY " mm = '" || mm || "'"
SAY " ss = '" || ss || "'"
A = "one two three four five six seven"
SAY "A = ""one two three four five six seven"""
SAY "If you keep executing: 'PARSE VAR A . A'"
SAY "THEN..."
DO WHILE A \= ""
SAY " A= '" || A"'"
PARSE VAR A . A
END
-------------------------------------------------------
If TIME() = '13:39:39'
After: 'PARSE VALUE TIME() WITH hh ":" mm ":" ss'
hh = '13'
mm = '39'
ss = '39'
A = "one two three four five six seven"
If you keep executing: 'PARSE VAR A . A'
THEN...
A= 'one two three four five six seven'
A= ' two three four five six seven'
A= ' three four five six seven'
A= ' four five six seven'
A= ' five six seven'
A= ' six seven'
A= ' seven'
PARSE Examples
Here are a couple of other examples of PARSE.
In the first we are execution the TIME function and splitting the output,
all in one instruction. In the second example we are breaking down a
string word by word. We do this be coding the input string as one
of the outputs. We are just take off the first word on each
execution as you can see.
PARSE UPPER VAR InVar OutVar
where: UPPER = converts all chars to upper case.
PARSE UPPER PULL
where: UPPER = converts all chars to upper case.
PULL = get line from input stream
PARSE UPPER ARG
where: UPPER = converts all chars to upper case.
ARG = gets arguments going into program or function
UPPER, PULL & ARG
We haven't seem these before.
UPPER can be coded after the word PARSE to ensure that we also
do a case conversion. Simple and useful.
In the second template here we have dropped the word 'OutVar',
now we are getting our input from a different place.
In the first line it is coming from the input stream.
Exactly what that is will depend on where you are running your REXX.
In second line ARG has been coded, here the input is coming from
arguments, either passed to a function if it's coded in a function,
or arguments passed to the program if its not coded in a function.
Now here something interesting, coding PULL actually gives you a
PARSE UPPER PULL and coding ARG you get an
PARSE UPPER ARG!
TOP . .
BACK