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

If you use a string in a template, parsing checks the input data for a matching string. When assigning data into variables, parsing generally skips over the part of the input string that matches the string in the template.
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

When you do not know in advance what string to specify as separator in a template, you can use a variable enclosed in parentheses.
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.

An unsigned integer or an integer with the prefix of an equal sign (=) separates the data according to absolute column position . The first segment starts at column 1 and goes up to, but does not include, the information in the column number specified. Subsequent segments start at the column numbers specified.
quote = 'Ignorance is bliss.'
....+....1....+....2
PARSE VAR quote part1 5 part2
/* part1 contains 'Igno' */
/* part2 contains 'rance is bliss.' */
The following code has the same result:
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.

When each variable in a template has column numbers both before and after it, the two numbers indicate the beginning and the end of the data for the variable.
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. */
A signed integer in a template separates the data according to relative column position . The plus or minus sign indicates movement right or left, respectively, from the starting position. In the next example, remember that part1 starts at column 1 (by default because there is no number to indicate a starting point).
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).
To provide more flexibility, you can define and use variable numeric patterns in a parsing instruction. To do this, first define the variable as an unsigned integer before the parsing instruction. Then, in the parsing instruction, enclose the variable in parentheses and specify one of the following before the left parenthesis:
  • 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.
(Without + , - , or = before the left parenthesis, the language processor would consider the variable to be a string pattern.) The following example uses the variable numeric pattern movex.
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.



ways.html | Timestamp icon Last updated: Monday, 5 June 2017