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