roboticus has asked for the wisdom of the Perl Monks concerning the following question:
I joined back in March, but this is my first question to SoPW. A fellow monk suggested that I read Resorting to Sorting to get a handle on some alternate sorting idioms. While doing so, I'm having a spot of trouble with Exercise 2 in the Orcish Manuever section.
My code compiles cleanly, and it runs, but it's giving me incorrect results. The results I want are:
I don't want to spoil the exercise for those who haven't worked it yet, so I've spoilerized it...Unsorted List:beta, apple, gamma globulin, alpha, zeta, gesorenplatz!! +, gamma, delta, foolish mortal, Sorted List:, beta, zeta, alpha, apple, delta, gamma, foolish mortal, +gamma globulin, gesorenplatz!!
#!/usr/bin/perl -w # # Orcish_Ex.pl # # Exercises from the "Orcish Sorting" section of perlmonks tutorial no +de # "Resorting to Sorting" (id=node 128722). # # Exercise 1: # Q: Why should you make the caching hash viewable only by the # sorting function? How is this accomplished? # # A: To avoid namespace pollution. By declaring locally in a bl +ock. # (My answers. I haven't checked 'em yet.) # NOTE: while coding it up, I started to suspect that %cache sho +uld # be nulled out, or successive sort calls could botch it up. Is + it # a problem? See Orcish_Ex_test.pl # # Exercise 2: # Use Orcish Manuever to sort a list of strings in the same way +as # described in Naive section, Exercise 1. # # Note: Yes, there is an intentional blank line at the end so we can s +ee # where the empty string will sort to! # use strict; use warnings; # Orcish section { my %cache; sub len_or_str { my $va = ($cache{$a} ||= [ length($a), substr($a,0,5) +]); my $vb = ($cache{$b} ||= [ length($b), substr($b,0,5) +]); return ($va->[0] <=> $vb->[0]) or ($va->[1] cmp $vb->[1]); } sub print_cache { print "Cache after sort:\n"; for my $k (keys %cache) { print "\t", $k, " => [", $cache{$k}->[0], ", " +, $cache{$k}->[1], "]\n"; } } } sub print_msg_list { my $msg = shift @_; print $msg, join(", ", @_), "\n"; } my @unsorted = <DATA>; chomp @unsorted; &print_msg_list("Unsorted List:", @unsorted); my @sorted = sort len_or_str @unsorted; &print_msg_list("Sorted List:", @sorted); &print_cache; __DATA__ beta apple gamma globulin alpha zeta gesorenplatz!! gamma delta foolish mortal
UPDATE: I forgot to wrap the code in code tags!
UPDATE: I linked "Orcish Manuever" since planetscape told me how.
TIA, roboticus
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sorting problem with Orcish Manuever
by japhy (Canon) on May 22, 2006 at 12:26 UTC | |
by bart (Canon) on May 22, 2006 at 12:39 UTC | |
by roboticus (Chancellor) on May 22, 2006 at 16:40 UTC | |
|
Re: Sorting problem with Orcish Manuever
by bart (Canon) on May 22, 2006 at 12:35 UTC | |
by roboticus (Chancellor) on May 22, 2006 at 16:31 UTC | |
by planetscape (Chancellor) on May 23, 2006 at 07:32 UTC | |
by roboticus (Chancellor) on May 23, 2006 at 11:32 UTC | |
|
Re: Sorting problem with Orcish Manuever
by liverpole (Monsignor) on May 22, 2006 at 12:27 UTC | |
|
SUMMARY: Sorting problem with Orcish Manuever
by roboticus (Chancellor) on May 23, 2006 at 11:49 UTC |