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.
In reply to Re: Crazy hash sorting issue.
by japhy
in thread Crazy hash sorting issue.
by rementis
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |