Templates containing positional (numeric) patterns
A positional pattern is a number that identifies the character position at which to split data in the source string. The number must be a whole number.
- A number with no plus (+) or minus (-) sign preceding it or with an equal sign (=) preceding it
- A variable in parentheses with an equal sign before the left parenthesis. (See Parsing with Variable Patterns for details on variable positional patterns.)
The number specifies the absolute character position at which to split the source string.
variable1 11 variable2 21 variable3
The numbers 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 1 through 10 of the source string into variable1
- Puts characters 11 through 20 into variable2
- Puts characters 21 to the end into variable3.
character positions:
1 11 21 40
┌──────────┬──────────┬────────────────────┐end of
FIELDS: │LASTNAME │FIRST │PSEUDONYM │record
⋘──────────┴──────────┴────────────────────┘
The
following example uses this record structure. /* 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 */
The source string is first split at character position 11 and at position 21. The language processor assigns characters 1 to 10 into lastname, characters 11 to 20 into firstname, and characters 21 to 40 into pseudonym.
1 lastname 11 firstname 21 pseudonym
instead
of lastname 11 firstname 21 pseudonym
Specifying
the 1 is optional.lastname 11 first 21 pseudonym
lastname =11 first =21 pseudonym
A relative positional pattern is a number with a plus (+) 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 Parsing with Variable 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 */
Blanks
between the sign and the number are insignificant. Therefore, +10 and + 10 have
the same meaning. Note that +0 is a valid relative positional pattern.│ │ │lastname 11│ │firstname 21 │ │ pseudonym │
│ │ │lastname +10│ │firstname + 10│ │ pseudonym │
⋘──┬───┘ ⋘──────┬─────┘ ⋘──────┬───────┘ ⋘─────┬─────┘
│ │ │ │
(Implied Put characters Put characters Put characters
starting 1 through 10 11 through 20 21 through
point is in lastname. in firstname. end of string
position (Non─inclusive (Non─inclusive in pseudonym.
1.) stopping point stopping point
is 11 (1+10).) is 21 (11+10).)
/* 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" */
The
absolute positional pattern 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 */
In
the previous example, the relative positional pattern -3 backs
up to the first character in the source string.│ 2 │ │var1 4 │ │ 1 │ │var2 2│ │ 4 var3 5│ │11 var4 │
│ 2 │ │var1 +2 │ │ ─3 │ │var2 +1│ │+2 var3 +1│ │+6 var4 │
⋘──┬──┘ ⋘───┬────┘ ⋘──┬───┘ ⋘───┬───┘ ⋘────┬─────┘ ⋘───┬────┘
│ │ │ │ │ │
Start Non─ Go to 1. Non─ Go to 4 Go to 11
at 2. inclusive (4─3=1) inclusive (2+2=4). (5+6=11).
stopping stopping Non─inclusive
point is 4 point is stopping point
(2+2=4). 2 (1+1=2). is 5 (4+1=5).
/* Making multiple 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. */
Combining patterns and parsing into words
/* 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 had 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.
/* 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'