next up previous
Next: About this document Up: No Title Previous: No Title

Lab Assignment #2

Due by 10:30 p.m. on Tuesday, January 26.

A. General

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.

  1. Write a script 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.

  2. Write a script 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.)

  3. The data file ~/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.)

  4. A dice game.

    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.)

  5. Do the UNIX command 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.


next up previous
Next: About this document Up: No Title Previous: No Title

Kirby A. Baker
Sun Jan 24 15:28:27 PST 1999