tombmbdil has asked for the wisdom of the Perl Monks concerning the following question:
# sample data my %network = ( 10.1.0.1, { 'name' => 'vlan 2' } 10.2.0.2, { 'name' => 'vlan 3' } ); foreach my $network (sort { by_num($networks{$a}->{'name'}) <=> by_num +($networks{$b}->{'name'}) or $a cmp $b } keys %networks ) { # do stuff... } # Extract the number from a string # Used to sort Network names by vlan number, rather than alphabeticall +y sub by_num { my $key = shift; $key =~ /(\d+)/; return $1; }
I think this should work, but I was wondering if there might be a more obvious/elegant solution.#### untested/concept code below # set the following to either 'alpha' or 'num' $sortby= 'alpha'; my %sort_network; $sort_network{'alpha'} = sub { $a->{'name'} cmp $b->{'name'} }; $sort_network{'num'} = sub { by_num($a->{'name'}) <=> by_num($b->{'nam +e'} ) or $a->{'name'} cmp $b->{'name'}; foreach my $network (sort &$sort_network{$sortby} keys %networks ) { # do stuff... }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Variable Subroutine
by broquaint (Abbot) on May 27, 2003 at 17:09 UTC | |
by tombmbdil (Beadle) on May 27, 2003 at 17:20 UTC | |
|
Re: Variable Subroutine
by Coplan (Pilgrim) on May 27, 2003 at 17:27 UTC |