Linguistics 408/508 |
Fall 2003 |
Hammond |
Handout 4
Overview
- Questions from last time
- Control structure practice
- Input-Output (continued)
Control structure practice
- Last time we wrote a little program to print out all the words entered
on the command-line with spaces between each word. There were several ways to
do this, but here's the simplest
one.
- Hong pointed out that this actually puts spaces after
every word. First, let's see that this is true by printing
something at the end of the sentence. Here's a revision of the first program
that does this (example).
- How would we get rid of that last space? There are several ways to do
this; here's one (example).
Input-Output
- Prompt. We've already treated prompt output: the
print
command. Prompt input is straightforward and requires two notions:
handles and the IO operator < >
. The handle
STDIN
represents "standard input", what I'm calling
the prompt. To read a line from the prompt, you simply use the value returned
by <STDIN>
(example).
- File. File input and output uses the same basic ideas, but is a little
more complex. File input involes first creating a handle from the filename
with the
open
command, then reading from it, and finally closing
the handle with close
(example).