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.