Tom Kelliher, CS29
Feb. 18, 1997
Announcements:
Write a program which interactively asks for a file name, then opens and
reads the file, line by line. Each line should be echoed to the screen,
with a line number and a tab (\t
) preceding it. (Advanced: In
addition, output the number of words in the file. A word is defined to be
a contiguous sequence of non-space characters.)
Outline:
Use the following as the first line of your perl programs:
#!/usr/contrib/bin/perl -w
$<varName>
Example:
#!/usr/contrib/bin/perl -w # This is a small perl program which demonstrates some arithmetic # operations: # o I/O. # o Assignment. # o Addition and division. # o == and < --- numeric comparison. # # The program reads n, the number of scores, then reads the n scores, then # prints the average score. print "Enter the number of scores: "; # Get the number of scores. $n = <STDIN>; if ($n == 0) # Error checking. { print "n cannot be zero.\n"; exit 1; # Return, abnormally, to shell. } if ($n < 0) # More error checking. Could be combined { # with previous check. print "n cannot be negative."; exit 1; } $sum = 0.0; # Initialize. for ($i = 1; $i <= $n; $i = $i + 1) # Read the n scores. { print "Enter score $i: "; $score = <STDIN>; $sum = $sum + $score; } $average = $sum / $n; # Compute average. print "\nThe average score is $average.\n"; # Print result. exit 0; # Return, normally, to shell.
What do the ;'s mean?
Features:
$i = 1; $n = <STDIN>; $sum = $sum + $score; $average = $sum / $n;
print "The average is $average.\n"; print $average;
Example:
#!/usr/contrib/bin/perl -w # This is a small perl program which demonstrates some string operations: # o I/O. # o Assignment. # o chop() # o Concatentation. # o eq --- string comparison. # # The program reads a first and last name, then prints the entire name. print "Enter given name: "; # Get first name. $first = <STDIN>; chop($first); # Remove "enter" character (newline) from # end of string. print "Enter family name: "; # Get last name. $last = <STDIN>; chop($last); $wholeName = $first . " " . $last; # Concatenate the strings. print "\nYour name is $wholeName.\n"; # Print the name. if ($wholeName eq "Tom Kelliher") # String comparison. { print "\nSay, you're Tom Kelliher!\n"; } exit 0; # Return, normally, to shell.
Features:
"<string>"
. Variable substitution.
\n
, \\
, \"
, \t
$name = <STDIN>; $nameAgain = $name; $wholeName = $first . " " . $last;
$name = <STDIN>; print $name; print "Your name is $name.\n";
$name = <STDIN>; chop($name);
#!/usr/contrib/bin/perl -w # This is a small perl program which demonstrates some file I/O # operations: # o File handles. # o Opening a file for input and checking to see if it opened. # o Using an opened file. # o Closing a file. # # This program interactively reads a file name and then copies the contents # of that file to the screen. print "Enter file name: "; # Get the file name. $name = <STDIN>; chop($name); open(INPUT, $name) || # Try to open the file. die "Cannot open $name"; print "Contents of $name:\n"; while ($line = <INPUT>) # Print the file on the screen. { print $line; } close(INPUT); # Close the file. exit 0;
Features:
open(<HANDLE>, <filename>) || die "Could not open <filename>";
$line = <HANDLE>;
close(<HANDLE>);
Copy the file ~kelliher/pub/arith.pl
to your home directory. It is
the ``compute an average'' example. Modify it so that it computes the
geometric rather than the arithmetic average.