Приглашаем посетить
Ходасевич (hodasevich.lit-info.ru)

Workshop

Previous Table of Contents Next

Workshop

Quiz

1:

Given the following code, what's left in @A afterward:


@A=qw(oats peas beans);

shift @A;

push @A, "barley";

pop;


  1. oats peas beans

  2. beans barley

  3. peas beans barley

2:

What does printf("%18.3f", $a) do?

  1. It prints a floating-point number that is 18 characters wide: 15 to the left and 3 to the right of the decimal point.

  2. It prints a floating-point number 18 characters to the left of the decimal, a decimal point, and then 3 more digits.

  3. It prints a floating-point number that is 18 characters wide, 14 to the left and 3 to the right of the decimal point.

3:

If TR/a-z/A-Z/ is run against a string, will tr/A-Z/a-z/ restore the string to its original form?

  1. Yes, of course

  2. Probably not

Answers

A1:

c. The shift removed oats, and the push added barley to the end. The final pop was a ruse: No array was specified, so it popped something from the array @_ or from @ARGV, which causes nothing to happen to @A.

A2:

c. If you guessed a, you didn't count the decimal point, which occupies a position in the total (18=14+1+3).

A3:

b. The string "Rosebud" transformed by tr/a-z/A-Z/ becomes "ROSEBUD". TRying to transform it back with tr/A-Z/a-z/ yields "rosebud"—not the original string.

Activities

  • Rewrite the Hangman game from Hour 4 to use scalars instead of arrays. You can use substr to manipulate individual characters in the scalars.

  • Modify Listing 9.2 to read from a file instead of getting data from an array. Open the file, read the data into an array, and continue as normal. You will have to create the file on disk, of course.

    Previous Table of Contents Next