7.8 Exercises


The answers for all exercises can be found in Section A.6.

7.8.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.

7.8.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?"

7.8.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.

7.8.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 


Learning Perl Objects, References & Modules
Learning Perl Objects, References, and Modules
ISBN: 0596004788
EAN: 2147483647
Year: 2003
Pages: 199

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net