in reply to Re: Turning a hash into a line of links
in thread Turning a hash into a line of links
#!/usr/local/bin/perl -l # use strict; use warnings; my %sections = ( DMA => q{dma}, FST => q{dma/fst}, MRA => q{dma/mra}, BKS => q{bks}, MSU => q{dma/msu}, FMA => q{dma/fma}, FOMC => q{dma/FOMC}); my $thisKeyFirst = q{DMA}; my @sortedKeys = map { $_->[0] } sort { $b->[1] <=> $a->[1] || $a->[0] cmp $b->[0] } map { [$_, $_ eq $thisKeyFirst] } keys %sections; print for @sortedKeys;
which produces
DMA BKS FMA FOMC FST MRA MSU
It seems to me that it would be easier to manage that way. It you had a few special keys that fell into this category this method would still work but with a hash to hold the order of the specials, highest value first and keys not in specials hash getting zero. Something like (not tested)
my %specials = ( DMA => 2, MSU => 1); ... map { [$_, exists $specials{$_} ? $specials{$_} : 0] } keys %sections;
Cheers,
JohnGG
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Turning a hash into a line of links
by jbert (Priest) on Dec 13, 2006 at 10:40 UTC | |
by johngg (Canon) on Dec 13, 2006 at 11:36 UTC |