REXX String Handling
The REXX PARSE Command
The PARSE command can be used to break up a source string into substrings
based on a supplied template. A template consists of variable names plus
patterns which specify how to break up the source string. If only variable
names are provided, then the source string is separated on word boundaries.
A period (.) can be used as a place-holder if some data is to be ignored.
For example,
Parse Var x a b . c
will extract the first two words from variable x and store them into variables
a and b. The third word will be ignored. The remainder will be stored in variable
c.
However, data positions in the input string can also be used to break up the
source string. For example,
Parse Var y 5 a +8 b +4 c
will take 8 characters from variable y starting in position 5 and store
them into variable a, then store the next 4 characters from position 13
into variable b and, finally, any remaining characters after position 17
into variable c. This technique can be used to process data which contains
fixed length subfields.
Character strings can also be used as separators. For example,
Parse Var z 'key=' v ','
will take all characters in variable z from the first occurrence of the string
'key=' up to the next following occurrence of the string ',' and store that
into variable v.
The above template elements can be combined to perform more complex parsing
operations.
Working with WORDS
The WORDS function returns the number of words in an input variable. The
WORD function can be used to return a selected word. For example,
Do i = 1 to WORDS(w)
Say WORD(w,i)
End
will list all words from variable w one by one.
The SUBWORD function can be used to return multiple words beginning with
a specified word. For example,
Say SUBWORD(v, 2, 3)
displays the second through fourth words in variable v.
For More Information
Visit the REXX page here for more sources of REXX info.