MNTGETTOK: Extracting Tokens From a String Function

How to:

The Maintain function MNTGETTOK divides a character string into substrings, called tokens. In order to use MNTGETTOK, the data must have a specific character called a delimiter that occurs in the string and separates the string into tokens. MNTGETTOK returns the token specified by the token_number argument.

For example, you can use MNTGETTOK to extract individual values from a list separated by semi-colons, by designating the semi-colon as the delimiter.

To use this function, you must import the function library MNTUWS.

Note:

Syntax: How to Extract a Substring (Token)

Module Import(mntuws)
MNTGETTOK(infield,"delim",token_number)

where:

infield

Alphanumeric

Is the field containing the original character string or a character string enclosed in single or double quotation marks.

delim

Alphanumeric

Is the delimiter in the parent string enclosed in single or double quotation marks. If you specify more than one character, only the first character is used. The delimiter is not included in the token.

token_number

Integer

Is the number of the token to extract. If this argument is positive, the tokens are counted from left to right. If this argument is negative, the tokens are counted from right to left. For example, -2 extracts the second token from the right. If this argument is 0, the function returns spaces.

Example: Extracting Tokens From a String

MNTGETTOK extracts tokens from the variable length character string SKILLSTRING and stores the result in the variable length character string TOKENX. The delimiter is a blank space. The token number is based on the value of the counter variable i, which increments with each pass through the Repeat loop:

MAINTAIN                                          
MODULE IMPORT(MNTUWS)                             
SKILLSTRING/A0="Typing Steno Filing Bkkping";     
COMPUTE i/i2 = 1;                                 
TYPE "Job skills required are:"                   
REPEAT 6                                          
COMPUTE TOKENX/A0=MNTGETTOK(SKILLSTRING, ' ', i );
TYPE "<<TOKENX";                                  
COMPUTE i = i+1;                                  
ENDREPEAT                                         
END

The output is:

Job skills required are:
Typing                  
Steno                   
Filing                  
Bkkping                 

Example: Extracting the Zip Code From an Address

The following procedure against the EMPLOYEE data source retrieves the EMPINFO segment and the first instance of ADDRESS_LN3 for each employee, then extracts the last token (zip code) from ADDRESS_LN3:

MAINTAIN FILE EMPLOYEE                                    
MODULE IMPORT(MNTUWS)                                     
REPEAT ALL;                               
NEXT EMP_ID  INTO ESTACK                                  
IF FOCFETCH NE 0 THEN GOTO EXITREPEAT;                    
NEXT ADDRESS_LN3 INTO ASTACK                              
TYPE "<<ESTACK.FIRST_NAME  <<ESTACK.LAST_NAME";           
TYPE "<<ASTACK.ADDRESS_LN3";                              
COMPUTE ZIP/A0=MNTGETTOK(ASTACK.ADDRESS_LN3, " ", -1 );
TYPE "ZIP CODE IS: <<ZIP";                             
TYPE " ";                                                 
ENDREPEAT                                                 
END                                                       

The output is:

ALFRED     STEVENS    
NEW YORK NY 10001     
ZIP CODE IS: 10001    
                      
MARY       SMITH      
NEW YORK NY 10001     
ZIP CODE IS: 10001    
                      
DIANE      JONES      
NEW YORK NY  10001    
ZIP CODE IS: 10001    
                      
RICHARD    SMITH      
NEW YORK NY 10001     
ZIP CODE IS: 10001    
                      
JOHN       BANNING    
FREEPORT NY 11520     
ZIP CODE IS: 11520    
                      
JOAN       IRVING     
NEW YORK NY 10001     
ZIP CODE IS: 10001    
ANTHONY    ROMANS    
NEW YORK NY  10001   
ZIP CODE IS: 10001   
                     
JOHN       MCCOY     
NEW YORK NY 10001    
ZIP CODE IS: 10001   
                     
ROSEMARIE  BLACKWOOD 
NEW YORK NY  10001   
ZIP CODE IS: 10001   
                     
ROGER      MCKNIGHT  
NEW YORK NY 10001    
ZIP CODE IS: 10001   
                     
MARY       GREENSPAN 
NEW YORK NY  10001   
ZIP CODE IS: 10001   
                     
BARBARA    CROSS     
NEW YORK NY  10001   
ZIP CODE IS: 10001