Next / Previous / Index / ITC Help System / Publications / Site map / NM Tech homepage

An example of Icon parsing

Tech Computer Center logo

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.


Next: Using data structures in Icon
See also: Implicit scanning in Icon
Previous: Using the tab function for parsing in Icon
Site map
Index: Keyword index to help pages
Help: New Mexico Tech Information Technology and Communications: Help System
ITC Publications
To report a problem: File a ticket
Send mail to the User consultant on duty or call them at 575-835-5437
Home: About New Mexico Tech

John Shipman, john@nmt.edu
Last updated: 1998/02/08 23:15:39 UT
URL: http://www.nmt.edu/tcc/help/lang/icon/parsex.html
QR two-dimensional bar code