in reply to Crazy hash sorting issue.
I hope it's clear what I'm doing. I create a sieve by passing a list of patterns in the order of importance. Then I use that code reference (returned by sieve()) to "sort" a list. I can pass a code block as the first argument to determine what I do to the strings being passed to the sieve. In this case, even though I'm sorting the keys of the hash, I want to do the regex comparison to $tapes{$_}.sub sieve { my @pats = UNIVERSAL::isa($_[0], 'Regexp') ? @_ : map(qr/$_/, @_); sub { my %slots; $slots{$_} = [] for @pats, ""; my $cref = UNIVERSAL::isa($_[0], 'CODE') && shift; ITEM: for (@_) { my $k = $cref ? $cref->() : $_; for my $p (@pats) { push(@{ $slots{$p} }, $_), next ITEM if $k =~ /$p/; } push @{ $slots{""} }, $_; } return map @$_, @slots{@pats, ""}; } } my %tapes = (...); my $c5p5 = sieve(qr/-C5$/, qr/-P5$/); my @sorted_keys = $c5p5->(sub { $tapes{$_} }, keys %tapes);
P.S. this looks much prettier in Ruby.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Crazy hash sorting issue.
by Perl Mouse (Chaplain) on Nov 07, 2005 at 10:32 UTC | |
by japhy (Canon) on Nov 07, 2005 at 13:08 UTC | |
by Perl Mouse (Chaplain) on Nov 07, 2005 at 13:59 UTC |