Due by 10:30 p.m. on Tuesday, January 26.
A. General
#!/usr/local/bin/perl5 -w #Iona Lee 273-802-911, Problem 1.1, 1/17/99. This is my own work. # find_longest # This script finds the longest word in a text file with # one word per line. If there are two longest words, it # gives the first one.
and then your Perl script. As shown, it's good to include your name for the file and some description of what the script does. It's also good to put some date on your program; any reasonable date is acceptable, such as the date when you started it or the date when it is due. The idea is that if you look at the script two years from now you can tell when it was done.
Remember, it matters which line is which! The ``shell'' (UNIX
command interpreter) looks at line #1 to see if it starts with
#!
followed by the location of a program, and if it does
then the shell runs the rest of the file using that program. You
can see why this is handy; it means you can write a script in a
language such as Perl and then run it under its own name without
any separate compilation phase.
#
.
You can also stop the program early with a command exit;
When you have put in test output and are temporarily through with it, you can comment it out until you're really sure you won't need it any more.
substr
, with an explanation of how to use it.
0..5
but wonder whether 'a'..'z'
would
work; just make a test script
@v = 'a'..'z'; print "@v\n";
and see if you get a compilation error or a valid result. How
about a..z
without the quotes?
B. Reading
Look through the first chapter of the text to see an overview of Perl features. If you don't have the text, either check in Powell reserves or just read through parts of the on-line manual.
C. Problems
Make Perl scripts to do the following. Hand them in using the
submit
command.
dspace
that acts as a filter to
double-space a file, as follows: After each non-blank line,
copy it and add a blank line. For each blank line in the input,
copy it but don't add an extra blank line. So, if the input has
lines nonblank, nonblank, blank, blank, nonblank, then the
output has lines nonblank, blank, nonblank, blank, blank, blank,
nonblank, blank.
To test for nonblankness, a handy way is to use a test with
\S
, which stands for ``any nonblank character''. Notice
this is upper-case S; it's the opposite of \s
which
means ``any blank character'' (``white-space character'')--a space,
tab, or newline.
If you have read a line into the default string $_
, then
your test can involve
if ( /\S/ ){...}
, meaning ``if $_
contains any nonblank
character then ...''.
To test your program, try it on files in h
and also on
whatever Perl scripts you have around--they are text files too.
wcount
that imitates the UNIX
utility program wc
.
Specifically, if you give it either standard input or one or more file names as arguments, it takes the information and prints out the number of lines, number of words, and number of characters (including white-space characters). (Don't include options and don't bother with separate information for multiple files-just give the combined totals in that case.)
~/h/roster
is like the roster file obtained
from the Registrar for this course, but with made-up data and with
colon separators. A typical line looks like
902378210:CHEN, KENNETH YU:kennych@ucla.edu:PECO-INT:USR: :E
Make a script report
that, when run on this file, prints
out information for each student in the following multi-line format:
Name: CHEN, KENNETH YU
ID: 902-378-210
email: kennych@ucla.edu
major: PECO-INT
level: USR
Print a blank line after the information for each student. Don't bother with the last two data fields. Line up your output neatly (as shown) by using extra spaces. If any data field is blank, print ``(no information)'' instead of nothing. (``USR'' means Undergraduate, Senior; PECO is pre-econ, I think.)
Let's call this game ``alpha''. You pay $3 to play. You throw three dice (six-sided and fair). If they come up all different, you lose your money. If two or all three are the same, you get $1 back for each dot showing on the equal dice; for example, if they come up 5-2-5, you get 5 + 5 = $10 back, or for 2-2-2 you get $6 back; the only exception is that for 1-1-1 you get nothing back.
First, write a script ``alpha'' that plays this game once. It should choose three random integers from 1 to 6 and then write out the results in some reasonable one-line format. For example, the output might be
paid in $3, rolled 2-4-3, paid out $0, net to you $-3
To get a random real number from 0 to 6 (not including 6),
use rand(6)
; to get a random integer from 0 to 5, use
int(rand(6))
; to get a random integer from 1 to 6, use
1 + int(rand(6))
. To make a dollar sign inside double
quotes, use \$
Make your program executable and test it.
Now modify your program to do $n
plays of the game,
printing as before each time but also showing grand
totals of money in, money out, and net (positive or negative).
The number $n
should be obtained from an argument.
Include a test to make sure the user has given exactly one
argument and that the nonblank part of this argument consists of
digits; if the test fails then show the proper usage using the
verb!die! command.
Submit this modified version, after testing.
(Also, not for credit, try to decide whether this is a fair game, either by math or by experimentation.)
cp alpha alpha.cgi
to make a copy
of your alpha script. In the copy, take out the argument and
argument checking and build in $n = 20;
Now add print statements at the beginning and end so the output
looks like a valid HTML page. At the beginning of the output, have
the printout include the following (except my comment about blank lines):
Content-type: text/html
[two blank lines here]
<HTML>
<HEAD>
<TITLE> alpha game </TITLE>
<H3> The alpha game </H3>
</HEAD>
<BODY>
and then have the ``game'' part of the output and finally put more
print statements to produce
</BODY>
</HTML>
In addition, change the ``game'' part of the output so that before
each newline \n
it also prints <br>
which is the
HTML tag for ``break''.
Eventually we'll be including even more HTML ``tags'' than those used above, but browsers will work with even less than what is given here. The Title, for example, is not essential and is not visible, which is why a visible version of it is also included.
Now move the alpha.cgi
file to your directory public_html
,
if it isn't there already. Make it executable by all, using
chmod a+x alpha.cgi
You should be able to run it by pointing
your browser to the URL
http://www.pic.ucla.edu/~lee/alpha.cgi
(if your id is
lee
). To run it again, use the ``reload'' browser button.