Computer Science 3710, Winter '00: Take-home Midterm Answers


1. (10 marks)

Consider the following Perl program:

          #!/usr/bin/perl -w
          
          use strict;
          
          my(%acme);
          my(@e,@res);
          my($line,$a,$b,$c,$f);
          
          $line = <>;
          if ($line != /ab(ra|cad)abra) {
              @res = split(/\s+/,$line);
              ($a,$b,$c,$d) = @res;
              $acme{$line} = ($a**$c) + $d;
          }
          else if ($line =~ /(Open Sesame!){2}/) {
              (@e $f) = @res;
              $acme{$line} = ($e[0] * $e[3]) + $e[1];
          }
          else
              print "Unrecognized line\n" . $line;
          

This program contains 5 syntax errors. Circle each of these 5 errors in the code above and by each circle, write a short sentence describing the error.

Answer:

The five errors are: The corrected version of the code is here.


2. (15 marks)

Write Perl expressions or single statements that perform the following computations.

Answer:

Those wishing to investigate this further may be interested in the following code and output-script file.


3. (10 marks)

Consider the following Perl program:

          #!/usr/bin/perl
          
          use strict;
          
          my(@list) = (5, "Todd", 3, 2);
          
          $list[2] += 4;
          print "M1: ".$list[0]." ".$list[1]." ".$list[2]." ".$list[3]."\n";
          ($list[1], $list[3]) = funky(@list);
          print "M2: ".$list[0]." ".$list[1]." ".$list[2]." ".$list[3]."\n";
          
          ## End of main program
          
          
          sub funky {
            my($v1, $v2, $v3) = @_;
          
            print "F1: "." ".$v1." ".$v2." ".$v3.
                         " ".$_[0]." ".$_[1]." ".$_[2]." ".$_[3]."\n";
            $_[0]++;
            print "F2: "." ".$v1." ".$v2." ".$v3.
                         " ".$_[0]." ".$_[1]." ".$_[2]." ".$_[3]."\n";
            $v3 = "Bill";
            print "F3: "." ".$v1." ".$v2." ".$v3.
                         " ".$_[0]." ".$_[1]." ".$_[2]." ".$_[3]."\n";
            return($v3, $v2);
          }  ## Of function FUNKY

Print all five lines of output of this program.

Answer:

The lines of output are:

          M1: 5 Todd 7 2
          F1:  5 Todd 7 5 Todd 7 2
          F2:  5 Todd 7 6 Todd 7 2
          F3:  5 Todd Bill 6 Todd 7 2
          M2: 6 Bill 7 Todd

Those wishing to investigate this further may be interested in the following code and output-script file.


4. (15 marks)

Consider a text file in which each line has the format

          pattern#replacement#text

where pattern, replacement, and text are all strings over whitespace, letters, digits, and non-meta-character punctuation marks, i.e., there are no embedded pattern-matching meta-characters.

    (a) (10 marks)

    Give Perl code for a subroutine process that takes as its only parameter a list in which each list-element is a line with the format described above, processes each line to create the string consisting of string text in which all occurrences of string pattern have been replaced by string replacement, and returns the list in which each list-element is a line of the processed text.

    (b) (5 marks)

    Give Perl code for a main program that reads all lines of text from a file whose name is given as a command-line argument (where each line of this file has the format described above), stores this text in a list, passes this list for processing to subroutine process defined in part (a) of this question, and prints out (line by line) the processed version of this text returned by subroutine process.

Answer:

The program consisting of answers for parts (a) and (b) can be found here.


Created: March 21, 2000
Last Modified: March 21, 2000