sschneid has asked for the wisdom of the Perl Monks concerning the following question:
Hi again, monks!
I'm having a heck of a time trying to sort a hash. Here are the details:
First off, for reasons uncontrollable by me, I'm being provided a hash containing the contents of /etc/passwd in the following format:
$etcpw = { 'blah' => 'blah:x:3:1:blah:/home/blah:/sbin/nologin', 'tra' => 'tra:x:1:1:tra:/home/tra:/sbin/nologin', 'la' => 'la:x:2:1:la:/home/la:/sbin/nologin' };
My objective is to sort it based on UID, but the following (my best attempt) doesn't quite seem to work:
map { print "$etcpw->{$_}\n"; } sort { ($etcpw->{$a} =~ /:(\d+)/) <=> ($etcpw->{$b} =~ /:(\d+)/) } k +eys %{$etcpw};
I imagine it's probably pretty obvious, but I'm banging my head against the monitor trying to figure it out. Can someone tell me where I'm going wrong?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hash sorting (by value) woes
by dragonchild (Archbishop) on Feb 24, 2005 at 16:59 UTC | |
by sschneid (Deacon) on Feb 24, 2005 at 17:05 UTC | |
by ikegami (Patriarch) on Feb 24, 2005 at 17:14 UTC | |
by Roy Johnson (Monsignor) on Feb 24, 2005 at 17:33 UTC | |
by ikegami (Patriarch) on Feb 24, 2005 at 17:42 UTC | |
| |
|
Re: Hash sorting (by value) woes
by jonadab (Parson) on Feb 24, 2005 at 17:14 UTC | |
|
Re: Hash sorting (by value) woes
by RazorbladeBidet (Friar) on Feb 24, 2005 at 17:03 UTC | |
by dragonchild (Archbishop) on Feb 24, 2005 at 17:07 UTC |