Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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.

Replies are listed 'Best First'.
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 letters it will use the cmp and if you run perl 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 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.
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__ $ ./ letters baz: 199 foo: 3 bar: 42 $ ./ numbers foo: 3 bar: 42 baz: 199

Log In?

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
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2017-05-23 04:35 GMT
Find Nodes?
    Voting Booth?