in reply to Re: Crazy hash sorting issue.
in thread Crazy hash sorting issue.
You can also use a sieve. It's potentially faster than a complicated sort() algorithm to place things matching certain patterns before others.The code you post maybe faster than a 'sort', but compared to your "sieve", the sort needed is very simple.
I fail to understand the reason of this awfully complicated code. For this case, I'd just iterate over the hash twice:
If you want to iterate once, you need additional storage:my ($k, $v); $v =~ /C5$/ and print "$k $v\n" while ($k, $v) = each %tapes; $v =~ /P5$/ and print "$k $v\n" while ($k, $v) = each %tapes;
Although you can reduce storage costs by printing the first set while iterating:my (@C5, @P5); while (my ($k, $v) = each %tapes) { $v =~ /C5$/ ? push @C5, "$k $v\n" : push @P5, "$k $v\n"; } print @C5, @P5;
my @P5; while (my ($k, $v) = each %tapes) { $v =~ /C5$/ ? print "$k $v\n" : push @P5, "$k $v\n"; } print @P5;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Crazy hash sorting issue.
by japhy (Canon) on Nov 07, 2005 at 13:08 UTC | |
by Perl Mouse (Chaplain) on Nov 07, 2005 at 13:59 UTC |