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

Reading an input line in Icon

Tech Computer Center logo

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 strings 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: 1996/01/06 21:12:57 UT
URL: http://www.nmt.edu/tcc/help/lang/icon/simpleread.html
QR two-dimensional bar code