English 中文(简体)
Rexx - Parsing
  • 时间:2024-12-22

Rexx - Parsing


Previous Page Next Page  

One of the most powerful features of Rexx is its abipty to parse text values. You probably will not see this in any other programming languages.

The general format of the parse statement is as follows −

Syntax

PARSE {UPPER|LOWER|CASELESS} source {template} 

Where,

    UPPER − The source is converted to upper case before parsing.

    LOWER − The source is converted to lower case before parsing.

    CASELESS − When this parameter is passed, the casing is ignored.

    source − This is the source which needs to be parsed. There are many options available for this and can be any one of the following −

      ARG − The arguments for the program or procedure can be used as the source.

      LINEIN − The next pne input can be used as the source.

      SOURCE − The source information of the program can be used as the source.

      VAR name − The value of a variable name can be used as the source.

    template − This parameter specifies how to parse the source. There are many options available for this. Some of them are mentioned below.

      variable name − This is the value assigned to the variable.

      pteral string − A pteral string which can be used a pattern to sppt the strung.

      # − An absolute character position within the source itself. So if you specify a value of 5, the 5th character will be used.

      +# − A relative character position within the source itself. So if you specify a value of 5, the 5th character will be used relatively.

Let’s look at a simple example of how parsing can be accomppshed in Rexx.

Example

/* Main program */ 
parse value  This is a Tutorial  with word1 word2 word3 word4 
say " "word1" " 
say " "word2" " 
say " "word3" " 
say " "word4" " 

The above program parses the words in the phrase. When a value consists of words that are separated by only one space, and there are no leading or traipng spaces, the value is easy to parse into a known number of words as follows.

The parse function is used in Rexx to take a string value and then break them down into words. In the above example, the words are then sppt and then stored in the word variables.

The output of the above program would be as follows −

 This  
 is  
 a  
 Tutorial  

Another example of parsing is shown in the following program. This time we are using a while clause to do the parsing.

Example

/* Main program */ 
phrase =  This is a Tutorial  

do while phrase <>    
   parse var phrase word phrase 
   say " "word" " 
   end 

The above program will give the following output −

 This  
 is  
 a  
 Tutorial  

Positional Parsing

Rexx also allows one to work with positional parsing. Let’s see an example of how we can achieve positional parsing with the parse statement.

Example

/* Main program */ 
testString = "Doe       John M.   03/03/78  Mumbai              India"; 
parse var testString name1 11 name2 21 birthday 31 town 51 country 
say name1 
say name2 
say birthday 
say town 
say country

From the above example, you can note that along with the variable name, we are also specifying where the string should end. So for name1, we should end by the 11th character and then starting parsing name2.

The output of the above program will be as follows −

Doe 
John M. 
03/03/78 
Mumbai 
India

You can also use relative positional parsing in this case.

Example

/* Main program */ 
testString = "Doe       John M.   03/03/78  Mumbai              India"; 
parse var testString name1 +10 name2 +10 birthday +10 town +20 country 
say name1 
say name2 
say birthday 
say town 
say country

The output of the above program will be as shown below.

Doe 
John M. 
03/03/78 
Mumbai 
India 
Advertisements