## CIS 33A Programming in PERL Assignment A

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 can be done without a computer (although you can always check your result with a short Perl program). For exercises, hand in your answers only; don't hand in code.
• Programming Problems are to be done on the computer. For Programming Problems, hand in your your source code and program output.

### Exercises

#### Exercise A1

Show the result of the following expressions:

1. 23 + "7"
2. 14 * "A23"
3. 3 ** 4
4. "Hello" . "4"
5. 24 / 6

#### Exercise A2

Show the result of the following expressions:

1. "Hello \UPerl\E lovers"
2. "Is this \n a line\n or two\n"
3. 'What is \'this\' '
4. 'What is this \$x'

#### Exercise A3

What is the value of \$x and \$y after the following statements?
``` \$x = 4; \$x++; \$y += \$x + 3; ```

#### Exercise A4

Which of the following expressions evaluate to true, and which evaluate to false?

1. 4 < "23A"
2. -14 < "-13"
3. "14" != 14
4. 0 >= "AB"

#### Exercise A5

What is the value of each of the following expressions?

1. 1 <=> 7
2. 2 <=> "H"
3. 4 cmp 7
4. "4" <=> undef

#### Exercise A6

Which of the following expressions evaluate to true, and which evaluate to false?

1. "Hi" && 2
2. "Hi" || 2
3. "00" && 4
4. "00" || "0"

#### Exercise A7

The following statements set the values:

• \$x = 3;
• \$y = 0;
• \$z = -4;

Then, which of the following expressions evaluate to true, and which evaluate to false?

1. !( \$x < \$y )
2. !( \$x >= \$y )
3. !( (4 + 5 * \$y) >= \$z -2 ) && ( \$z - 2 )
4. !(\$x) && !(\$y) || !(\$z)

#### Exercise A8

Code a single statement, which uses the `&&` operator, and produces the same results as this code:

```  if ( \$x < 10 )
{
\$x++;
}
```

#### Exercise A9

Code a single statement, which uses the `||` operator, and produces the same results as this code:

```  unless ( \$x < 10 )
{
\$x++;
}
```

### Programming problems

#### Problem A10

Write a program that reads in three numbers:
Ask the user to input a decimal number as the first number.
Ask the user to input an octal number without the leading 0 as the second number.
Convert each number to decimal.
Print the following output, with description of each number:
Print the decimal value of the first number.
Print the octal and then the decimal value of the second number.
Print the hexadecimal and then the decimal value of the third number.
Use the following test data, in order:   1234   123   12

#### Problem A11

In this problem, do not use a loop.
Write a program that reads one string and prints it as many lines as its length with each copy on a separate line (The \n should not he considered when calculating the length).
For example, if the string is "world", the output should be as shown below (the length is 5):

```world
world
world
world
world
```

#### Problem A12

Write a program that has a long string given which contains at least three 'e'
The program finds the positions of the first, second, and third "e" in the string and prints the results.
Test the program with the data:
\$string = 'elephants eat a large quantity of food';

#### Problem A13

Write a program that has a long string given.
The program extracts the first five characters and the last five characters. and prints the results.
Test the program with the data:
\$string = 'elephants eat a large quantity of food';

#### Problem A14

Write a program that, given three strings, uses string comparisons to select and print the smallest in ASCII sort order. Do not use the sort function.

#### Problem A15

Write a program that, given an integer between 1 and 12, prints the corresponding month of the year (January, February, ...).

#### Problem A16

Write a program that, given a floating-point number, prints the number rounded to two digits.
For example. 14.334 will be rounded to 14.33 but 14.336 will be rounded to 14.34.