An example of Icon parsing |
|
Here is an extended example that demonstrates parsing in Icon. Suppose you want to parse a file describing types of plants, with lines that look like this:
00168 poison ivy
that is, each line starts with a number, then some blank space, then
the English name of the plant. Here is a program that
breaks the line up into the two data fields and prints the
output in parentheses, like this: (00168)(poison ivy)
procedure main()
while read() ? # Read each line and make it the subject
{ number := tab ( many ( &digits ) ); # Initial digits
tab ( many ( ' ' ) ); # Skip over any spaces
name := tab ( 0 ); # Grab the rest of the line
write ( "(", number, ")(", name, ")" );
}
end
Note that ``tab(0)'' means: move the position to
the end of the subject (position 0---see
`How Icon positions are numbered'), and return all the characters between
the current position and there.