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


Reading an input line in Icon

The ``read()'' function returns the next input line as a string, or fails if there are no more input lines. Here is a simple program that just reads and writes one line:

    procedure main()
      local line                # A variable to hold the line

      if  ( line  :=  read ( ) )  then   # Try to get the line
        write ( line )                   # Success: write it
      else                               # Failure: complain
        write ( "*** The input was empty! ***" );
end
Here is an ``identity filter'' program, which copies its input to its output:
    procedure main()
      while write ( read ( ) );
    end
The ``read()'' function is called repeatedly. Every time it succeeds in reading a new line, its result is sent to the ``write()'' procedure to be written. When there are no more input lines, ``read()'' fails, which causes ``write()'' to fail (because the evaluation of its argument failed), which terminates the ``while'' loop.
Next: How Icon positions are numbered
See also: Using the Icon programming language
Previous: Using strings in Icon

John W. Shipman, john@nmt.edu