Product SiteDocumentation Site

9.4. Parsing with Variable Patterns

You might want to specify a pattern by using the value of a variable or expression instead of a fixed string or number. You do this by placing an expression in parentheses. This is a variable reference. Whitespace characters are not necessary inside or outside the parentheses, but you can add them if you wish.
The template in the next parsing instruction contains the following literal string pattern ". ".
parse var name fn  init ". " ln
Here is how to specify that pattern as a variable string pattern:
strngptrn=". "
parse var name fn init (strngptrn) ln
If no equal, plus sign, minus sign, >, or < precedes the parenthesis that is before the variable name, the character string value of the variable is then treated as a string pattern. The expression can reference variables that have been set earlier in the same template.
Example:

Example 9.16. Combining variables in patterns

/* Using a variable as a string pattern                          */
/*  The variable (delim) is set in the same template             */
SAY "Enter a date (mm/dd/yy format). =====> " /* assume 11/15/98 */
pull date
parse var date month 3 delim +1 day +2 (delim) year
/* Sets: month="11"; delim="/"; day="15"; year="98"  */

If an equal, a plus, a minus sign, > or < precedes the left parenthesis, the value of the expression is treated as an absolute, relative positional, or length positional pattern. The value of the expression must be a positive whole number or zero.
The expression can reference variables that have has been set earlier in the same template. In the following example, the first two fields specify the starting-character positions of the last two fields.
Example:

Example 9.17. Combining variables an positional patterns

/* Using a variable as a positional pattern                      */
dataline = "12 26 .....Samuel ClemensMark Twain"
parse var dataline pos1 pos2 6 =(pos1) realname =(pos2) pseudonym
/* Assigns: realname="Samuel Clemens"; pseudonym="Mark Twain"    */

The positional pattern 6 is needed in the template for the following reason: Word parsing occurs after the language processor divides the source string into substrings using patterns. Therefore, the positional pattern =(pos1) cannot be correctly interpreted as =12 until after the language processor has split the string at column 6 and assigned the blank-delimited words 12 and 26 to pos1 and pos2, respectively.