Perl: the Markov chain saw PerlMonks

by james2vegas (Chaplain)
 on Aug 08, 2009 at 01:11 UTC Need Help??

```  my %sorts = (
'case sensitive forward'             => sub { \$_[0] cmp \$_[1] },
'case insensitive forward'           => sub { uc \$_[0] cmp uc \$_[1
+] },
'case sensitive backward'            => sub { \$_[1] cmp \$_[0] },
'case insensitive backward'          => sub { uc \$_[1] cmp uc \$_[0
+] },
'case sensitive forward reverse'     => sub { reverse(\$_[0]) cmp r
+everse(\$_[1]) },
'case insensitive forward reverse'   => sub { uc reverse(\$_[0]) cm
+p uc reverse(\$_[1]) },
'case sensitive backward reverse'    => sub { reverse(\$_[1]) cmp r
+everse(\$_[0]) },
'case insensitive backward reverse'  => sub { uc reverse(\$_[1]) cm
+p uc reverse(\$_[0]) },
);

# 5.10+ only
#sub do_sorts {
#   state %cache;
#   my \$res = \$cache{\$_[0], \$_[1]};
#   return if defined(\$res);
#   foreach my \$sort (@{\$_[2]}) {
#     \$res ||= \$sorts{\$sort}->(@_)
#   }
#   \$cache{\$_[0], \$_[1]} = \$res
#}
{
my %cache;
sub do_sorts {
my (\$first, \$second, @sort_list) = @_;
my \$res = \$cache{\$first, \$second};
return \$res if defined(\$res);
foreach my \$sort (@sort_list) {
\$res ||= \$sorts{\$sort}->(\$first, \$second);
last if \$res
}
\$cache{\$first, \$second} = \$res
}
}

my @list = qw(foo Bar baz Fuzz);

print join(', ', sort { do_sorts(\$a,\$b,('case sensitive forward revers
+e')) } @list)."\n";
XML-Parser/Parser.pm#HANDLERS

Create A New User
Chatterbox?
 choroba tried to solve Week of Code 37 [choroba]: I was albe to solve the first two problems, plus hack the 5th [choroba]: No idea how to solve the rest.

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2018-04-21 19:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (81 votes). Check out past polls.

Notices?