News flash: I'm going to Australia in February, so this Web site may be switched off at any time.


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()
      every read() ?    # Read each line and make it the subject
      { number  :=  tab ( match ( &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: Using the Icon programming language
Previous: Using the tab function for parsing in Icon

John W. Shipman, john@nmt.edu