variable1 11 variable2 21 variable3
11
and 21
are absolute positional patterns. The number 11
refers to the 11th position in the input string, 21
to the 21st position. This template puts characters:
variable1
variable2
variable3
Example 9.9. Parsing stems
/* Parsing with absolute positional patterns in template */ record.1="Clemens Samuel Mark Twain " record.2="Evans Mary Ann George Eliot " record.3="Munro H.H. Saki " do n=1 to 3 parse var record.n lastname 11 firstname 21 pseudonym If lastname="Evans" & firstname="Mary Ann" then say "By George!" end /* Says "By George!" after record 2 */
lastname
, characters 11 to 20 to firstname
, and characters 21 to 40 to pseudonym
.
1 lastname 11 firstname 21 pseudonym
lastname 11 firstname 21 pseudonym
1
is optional.
lastname 11 first 21 pseudonym
lastname =11 first =21 pseudonym
+
) or minus (-
) sign preceding it. It can also be a variable within parentheses, with a plus (+
) or minus (-
) sign preceding the left parenthesis; for details see Section 9.4, “Parsing with Variable Patterns”.
Example 9.10. Parsing with relative positional patterns
/* Parsing with relative positional patterns in template */ record.1="Clemens Samuel Mark Twain " record.2="Evans Mary Ann George Eliot " record.3="Munro H.H. Saki " do n=1 to 3 parse var record.n lastname +10 firstname + 10 pseudonym If lastname="Evans" & firstname="Mary Ann" then say "By George!" end /* same results */
+10
and + 10
have the same meaning. Note that +0 is a valid relative positional pattern.
/* Backing up to an earlier position (with absolute positional) */ string="astronomers" parse var string 2 var1 4 1 var2 2 4 var3 5 11 var4 say string "study" var1||var2||var3||var4 /* Displays: "astronomers study stars" */
1
backs up to the first character in the source string.
/* Backing up to an earlier position (with relative positional) */ string="astronomers" parse var string 2 var1 +2 -3 var2 +1 +2 var3 +1 +6 var4 say string "study" var1||var2||var3||var4 /* same results */
-3
backs up to the first character in the source string.
/* Making several assignments */ books="Silas Marner, Felix Holt, Daniel Deronda, Middlemarch" parse var books 1 Eliot 1 Evans /* Assigns the (entire) value of books to Eliot and to Evans. */
>
) or (<
) preceding it. It can also be an expression within parentheses, with a (>
) or (<
) preceding the left parenthesis; for details see Section 9.4, “Parsing with Variable Patterns”.
Example 9.11. Parsing with relative positional patterns
/* Parsing with relative positional patterns in template */ record.1="Clemens Samuel Mark Twain " record.2="Evans Mary Ann George Eliot " record.3="Munro H.H. Saki " do n=1 to 3 parse var record.n lastname >10 firstname >10 pseudonym If lastname="Evans" & firstname="Mary Ann" then say "By George!" end /* same results */
>10
and > 10
have the same meaning. Note that >0 <0 and are valid length positional pattern.
Example 9.12. Parsing with length patterns
/* Parsing with length patterns in template */ line = "04Mark0005Twain" parse var line len +2 first >(len) len +2 middle >(len) len +2 last >(len) say '"'first'" "'middle'" "'last'"' -- displays "Mark" "" "Twain" /* parsing with relative patterns only */ parse var line len +2 first +(len) len +2 middle +(len) len +2 last +(len) say '"'first'" "'middle'" "'last'"' -- displays "Mark" "05Twain" "Twain"
Example 9.13. Parsing with length patterns
/* Parsing with length patterns in template */ parse value '12345.6789' with '.' digit <1 -- digit -> "5" /* parsing with relative patterns only */ parse value '12345.6789' with '.' -1 digit +1 -- digit -> "5"
Example 9.14. Combining string patterns
/* Combining string pattern and parsing into words */ name=" John Q. Public" parse var name fn init "." ln /* Assigns: fn="John" */ /* init=" Q" */ /* ln=" Public" */
fn init
ln
" John Q"
" Public"
John
has three leading blanks. All are removed because parsing into words removes leading and trailing blanks except from the last variable.
Q
has six leading blanks. Parsing removes one word-separator blank and keeps the rest because init
is the last variable in that section of the template.
" Public"
, parsing assigns the entire string into ln
without removing any blanks. This is because ln
is the only variable in this section of the template. (For details about treatment of whitespace characters, see Section 9.1, “Simple Templates for Parsing into Words”.)
Example 9.15. Combining positional patterns
/* Combining positional patterns with parsing into words */ string="R E X X" parse var string var1 var2 4 var3 6 var4 /* Assigns: var1="R" */ /* var2="E" */ /* var3=" X" */ /* var4=" X" */
var1 var2
var3
var4
"R E"
" X"
" X"
var1
receives "R"
; var2
receives "E"
. Both var3
and var4
receive " X"
(with a blank before the X
) because each is the only variable in its section of the template. (For details on treatment of whitespace characters, see Section 9.1, “Simple Templates for Parsing into Words”.)