CIS 33A Programming in PERL
Assignment B

Please staple the assignment together. At the top of the first page print, in this order: your first name, your last name, CIS33A, and the assignment letter. Print the exercise or problem number at the beginning of each exercise and problem. Assignments are due at the beginning of class, and will be marked down if turned in later.

Authorship note: These assignments have been adapted from assignments given by Clare Nguyen, who adapted them from the work of Behrouz Forouzan.

There are 2 types of problems.

Exercises

Exercise B1

Show the actual list in each case.

  1. (14 , (14 .. 17) , 22)
  2. (14 .. 22 , (9 .. 5) , 25)
  3. (14 , 14 , () , 32)
  4. (23 , 99 , (5 .. 7) )

Exercise B2

What is the value of the variable?

Exercise B3

What is the value of each variable?
(This is an interesting example, but is poor code.)

Exercise B4

Show the value of variables and the array.
(This is an interesting example, but is poor code.)

Exercise B5

Show the array.

Exercise B6

Show the value of the array after the execution of the following program segment:

Exercise B7

Show the value of the arrays after the execution of the following program segment:

Exercise B8

What would be printed from the following program segment?

  $\ = "##" ;
  $, = "?" ;
  @x = (2, 3, 4, 5, 6, 7) ;
  print ("@x" , "@x") ;

Exercise B9

What would he printed from the following program segment?

  while ( (2, 5, 1) - 1)
    {
    print ("hello\n") ;
    }

Exercise B10

What would he printed from the following program segment?

  @x = (3, 4, 8, 10) ;
  foreach (@x)
    {
    print ("hello\n") ;
    }

Exercise B11

What would he printed from the following program segment?

   foreach (2 , 4 , 6 , 8 , 10 , 12 , 14)
    {
    if($_ > 10 )
      {
      print ("$_\n") ;
      }
    last ;
    }

Exercise B12

What would he printed from the following program segment?

  foreach (2 , 4 , 6 , 8 , 10 , 12 , 9 , 14)
    {
    if($_ > 10 )
      {
      next ;
      }
    print ("$_\n") ;
    }

Exercise B13

What would he printed from the following program segment?

  $x = 1 ;
    {
    print ("$x\n") ;
    $x++ ;
    if ($x > 7)
      {
      last ;
      }
    redo ;
    }

Exercise B14

What would he printed from the following program segment?

  @x = (1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10) ;
  foreach (@x)
    {
    $x = pop (@x) ;
    $y = shift (@x) ;
    print ("$x $y \n") ;
    }

Exercise B15

What would he printed from the following program segment?

  @x = (1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10) ;
  foreach (@x)
    {
    $x = pop (@x) ;
    $x = shift (@x) ;
    push (@x, $x) ;
    }
  print ("@x\n") ;

Programming problems

Problem B16

Write the following problem, with the following restrictions:

Problem B17

Write the following problem, with the following restrictions:

Common problem statement for B16 and B17

The problem to be done twice as problem B16 and problem B17 follows:

Prompt the user to enter a sequence of numbers (between 1 and 20) and/or letters (lower or uppercase), each number or letter is separated by a space, and the length of the sequence can vary. The user ends the sequence with a "q" or "Q", followed by a space. (Assume that the user always enters a "q" or "Q" followed by a space.)

An example of user input is:
1 12 a 2 5 P Q

After reading in the input, the script will:

  1. Print to screen only the valid numbers in the input sequence (discard the letters and any number less than 1 or greater than 20), the numbers should be separated by a "+" (addition)
  2. print out the sum of all the numbers.
  3. if the user enters "q" or "Q" and then types in more characters, discard all the characters after the "q" or "Q". (Don't print out these characters, and don't add them to the sum.)
  4. if the user enters no valid numbers, print a statement indicating that no valid number was entered.

Your script output for the example above should be:
1+12+2+5 = 20

Test your script with the following input (6 different test cases):

1 12 a 2 5 P Q output should be: 1+12+2+5 = 20
1 3 A t 4 q output should be: 1+3+4 = 8
c 1 Q output should be: 1 = 1
a b c d Q output should be: no valid number entered   (or some similar error statement)
1 2 34 q 4 5 6 output should be: 1+2 = 3
22 q 5 6 output should be: no valid number entered   (or some similar error statement)