z/OS TSO/E REXX Reference
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Simple templates for parsing into words

z/OS TSO/E REXX Reference
SA32-0972-00

Here is a parsing instruction:
parse value 'time and tide' with var1 var2 var3
The template in this instruction is: var1 var2 var3. The data to parse is between the keywords PARSE VALUE and the keyword WITH, the source string time and tide. Parsing divides the source string into blank-delimited words and assigns them to the variables named in the template as follows:
var1='time'
var2='and'
var3='tide'
In this example, the source string to parse is a literal string, time and tide. In the next example, the source string is a variable.
/* PARSE VALUE using a variable as the source string to parse    */
string='time and tide'
parse value string with var1 var2 var3           /* same results */

(PARSE VALUE does not convert lowercase az in the source string to uppercase AZ. If you want to convert characters to uppercase, use PARSE UPPER VALUE. See Using UPPER for a summary of the effect of parsing instructions on case.)

All of the parsing instructions assign the parts of a source string into the variables named in a template. There are various parsing instructions because of differences in the nature or origin of source strings. (See Parsing instructions summary for a summary of all the parsing instructions .)

The PARSE VAR instruction is similar to PARSE VALUE except that the source string to parse is always a variable. In PARSE VAR, the name of the variable containing the source string follows the keywords PARSE VAR. In the next example, the variable stars contains the source string. The template is star1 star2 star3.
/* PARSE VAR example                                             */
stars='Sirius Polaris Rigil'
parse var stars star1 star2 star3             /* star1='Sirius'  */
                                              /* star2='Polaris' */
                                              /* star3='Rigil'   */
All variables in a template receive new values. If there are more variables in the template than words in the source string, the leftover variables receive null (empty) values. This is true for all parsing: for parsing into words with simple templates and for parsing with templates containing patterns. Here is an example using parsing into words.
/* More variables in template than (words in) the source string  */
satellite='moon'
parse var satellite Earth Mercury               /* Earth='moon'  */
                                                /* Mercury=''    */
If there are more words in the source string than variables in the template, the last variable in the template receives all leftover data. Here is an example:
/* More (words in the) source string than variables in template  */
satellites='moon Io Europa Callisto...'
parse var satellites Earth Jupiter              /* Earth='moon'  */
                               /* Jupiter='Io Europa Callisto...'*/
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  '                                 */

In the source string, Earth has two leading blanks. Parsing removes both of them (the word-separator blank and the extra blank) before assigning var3='Earth'. Mars has three leading blanks. Parsing removes one word-separator blank and keeps the other two leading blanks. It also keeps all five blanks between Mars and Jupiter and both trailing blanks after Jupiter.

Parsing removes no blanks if the template contains only one variable. For example:
parse value '   Pluto   ' with var1        /* var1='   Pluto   '*/

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014