Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Specify sort method on the fly

by rethaew (Sexton)
on Jan 20, 2010 at 22:35 UTC ( #818578=perlquestion: print w/ replies, xml ) Need Help??
rethaew has asked for the wisdom of the Perl Monks concerning the following question:

Good day. I am in need of specifying a sorting method on an hash of hashes depending on the value of a variable. See this simplified code to see what I am trying to do.
if ($joe eq "numbers") {my $sortype='<=>';} if ($joe eq "letters") {my $sortype='cmp';} #then go on to work on the hash foreach $key (sort { $hoh{$a}->{value} $sortype $hoh{$b}->{value} } ke +ys %hoa) { blah; }
Obviously this code fails, but you can see what I am trying do to. I couldn't find anything like this searching the archives here. Thanks for your help.

Comment on Specify sort method on the fly
Download Code
Re: Specify sort method on the fly
by eric256 (Parson) on Jan 20, 2010 at 22:44 UTC

    If you use a hash to hold different sort functions, then you can call that function from inside the sort block. There are probably better ways to do this, but this works.

    #!/usr/bin/perl use strict; use warnings; my $test = shift; my $sort = { 'numbers' => sub { $_[0] <=> $_[1] }, 'letters' => sub { "$_[0]" cmp "$_[1]" }, }; my @test = qw/1 2 3 4 a b 0 10 9 100/; print sort {$sort->{$test}->($a,$b)} @test;

    If you run this perl sort_test.pl letters it will use the cmp and if you run perl sort_test.pl numbers it will sort using <=>


    ___________
    Eric Hodges
      my $sort = { };? my %sort = ( );!

        I tend to use references always, dunno why, habit i suppose. I like the way they look?


        ___________
        Eric Hodges
Re: Specify sort method on the fly
by almut (Canon) on Jan 20, 2010 at 23:17 UTC

    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
Re: Specify sort method on the fly
by BrowserUk (Pope) on Jan 21, 2010 at 00:04 UTC

    What's wrong with the simple solution?

    my @sorted; if ($joe eq "numbers") { @sorted = sort { $hoh{$a}->{value} <=> $hoh{$b}->{value} } keys %hoa; } else { @sorted = sort { $hoh{$a}->{value} cmp $hoh{$b}->{value} } keys %hoa } foreach $key ( @sorted ) { blah; }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://818578]
Approved by hok_si_la
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-10-24 21:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (138 votes), past polls