Parsing with Patterns
The simplest template is a group of blank-separated variable names. This parses data into blank-delimited words. The preceding examples all use this kind of template. Templates can also contain patterns. A pattern can be a string, a number, or a variable representing either of these.
String
phrase = 'To be, or not to be?' /* phrase containing
comma */
PARSE VAR phrase part1 ',' part2 /* template containing comma */
/* as string separator */
/* part1 contains 'To be' */
/* part2 contains ' or not to be?' */
In this example, notice that the comma is not included with 'To be' because the comma is the string separator. (Notice also that part2 contains a value that begins with a blank. Parsing splits the input string at the matching text. It puts data up to the start of the match in one variable and data starting after the match in the next variable.
Variable
separator = ','
phrase = 'To be, or not to be?'
PARSE VAR phrase part1 (separator) part2
/* part1 contains 'To be' */
/* part2 contains ' or not to be?' */
Again, in this example, notice that the comma is not included with 'To be' because the comma is the string separator.
Number
You can use numbers in a template to indicate the column at which to separate data. An unsigned integer indicates an absolute column position. A signed integer indicates a relative column position.
quote = 'Ignorance is bliss.'
....+....1....+....2
PARSE VAR quote part1 5 part2
/* part1 contains 'Igno' */
/* part2 contains 'rance is bliss.' */
quote = 'Ignorance is bliss.'
....+....1....+....2
PARSE VAR quote 1 part1 =5 part2
/* part1 contains 'Igno' */
/* part2 contains 'rance is bliss.' */
Specifying the numeric pattern 1 is optional. If you do not use a numeric pattern to indicate a starting point for parsing, this defaults to 1 . The example also shows that the numeric pattern 5 is the same as =5.
quote = 'Ignorance is bliss.'
....+....1....+....2
PARSE VAR quote
1
part1
10 11
part2
13 14
part3
19 1
part4
20
/* part1 contains 'Ignorance' */
/* part2 contains 'is' */
/* part3 contains 'bliss' */
/* part4 contains 'Ignorance is bliss.' */
Thus,
you could use numeric patterns to skip over part of the data:
quote = 'Ignorance is bliss.'
....+....1....+....2
PARSE VAR quote
2
var1
3 5
var2
7 8
var3 var 4 var5
SAY var1||var2||var3 var4 var5 /* || means concatenate */
/* Says: grace is bliss. */
quote = 'Ignorance is bliss.'
....+....1....+....2
PARSE VAR quote part1 +5 part2 +5 part3 +5 part4
/* part1 contains 'Ignor' */
/* part2 contains 'ance ' */
/* part3 contains 'is bl' */
/* part4 contains 'iss.' */
+5
part2
means parsing puts into
part2
data
starting in column 6 (1+5=6).
+5 part3
means data
put into
part3
starts with column 11 (6+5=11), and
so on. The use of the minus sign is similar to the use of the plus
sign. It identifies a relative position in the data string. The
minus
sign
"backs up"
(moves to the left) in the data string.
quote = 'Ignorance is bliss.'
....+....1....+....2
PARSE VAR quote part1 +10 part2 +3 part3 -3 part4
/* part1 contains 'Ignorance ' */
/* part2 contains 'is ' */
/* part3 contains 'bliss.' */
/* part4 contains 'is bliss.' */
In this example,
part1
receives
characters starting at column 1 (by default).
+10 part2
receives
characters starting in column 11 (1+10=11).
+3 part3
receives
characters starting in column 14 (11+3=14).
-3 part4
receives
characters starting in column 11 (14-3=11).
- A plus sign ( + ) to indicate column movement to the right
- A minus sign ( - ) to indicate column movement to the left
- An equal sign ( = ) to indicate an absolute column position.
quote = 'Ignorance is bliss.'
....+....1....+....2
movex = 3 /* variable position */
PARSE VAR quote part5 +10 part6 +3 part7 -(movex) part8
/* part5 contains 'Ignorance ' */
/* part6 contains 'is ' */
/* part7 contains 'bliss.' */
/* part8 contains 'is bliss.' */
For more information about parsing, see Parsing.