Приглашаем посетить
Соллогуб (sollogub.lit-info.ru)

Section 9.9.  Exercises

Previous
Table of Contents
Next

9.9. Exercises

You can find the answers to these exercises in "Answers for Chapter 9" in the Appendix.

9.9.1. Exercise 1 [15 min]

Using the glob operator, a naive sort of every name in the /bin directory by their relative sizes might be written as:

my @sorted = sort { -s $a <=> -s $b } glob "/bin/*";

Rewrite this using the Schwartzian Transform technique.

If you don't have many files in the /bin directory, perhaps because you don't have a Unix machine, change the argument to glob as needed.

9.9.2. Exercise 2 [15 min]

Read up on the Benchmark module, included with Perl. Write a program that will answer the question "How much does using the Schwartzian Transform speed up the task of Exercise 1?"

9.9.3. Exercise 3 [10 min]

Using a Schwartzian Transform, read a list of words, and sort them in "dictionary order." Dictionary order ignores all capitalization and internal punctuation. Hint: the following transformation might be useful:

my $string = 'Mary-Ann';
$string =~ tr/A-Z/a-z/;       # force all lowercase
$string =~ tr/a-z//cd;        # strip all but a-z from the string
print $string;                # prints "maryann"

Be sure you don't mangle the data! If the input includes the Professor and the skipper, the output should have them listed in that order, with that capitalization.

9.9.4. Exercise 4 [20 min]

Modify the recursive directory dumping routine so it shows the nested directories through indentation. An empty directory should show up as:

sandbar, an empty directory

while a nonempty directory should appear with nested contents, indented two spaces:

uss_minnow, with contents:
  anchor
  broken_radio
  galley, with contents:
    captain_crunch_cereal
    gallon_of_milk
    tuna_fish_sandwich
  life_preservers


Previous
Table of Contents
Next