Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Specify sort method on the fly

by almut (Canon)
on Jan 20, 2010 at 23:17 UTC ( [id://818582]=note: print w/replies, xml ) Need Help??


in reply to Specify sort method on the fly

Not quite as fancy as Eric's version, but should get the job done, too:

#!/usr/bin/perl use strict; use warnings; my %hoh = ( bar => {value => 42}, baz => {value => 199}, foo => {value => 3}, ); my $joe = shift @ARGV; my $sortfunctions = { numbers => sub { $hoh{$a}{value} <=> $hoh{$b}{value} }, letters => sub { $hoh{$a}{value} cmp $hoh{$b}{value} }, }; my $sortfunc = $sortfunctions->{$joe}; foreach my $key (sort $sortfunc keys %hoh) { print "$key: $hoh{$key}{value}\n"; } __END__ $ ./818578.pl letters baz: 199 foo: 3 bar: 42 $ ./818578.pl numbers foo: 3 bar: 42 baz: 199

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://818582]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found