monkfan has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a code that to pick upper-N value of a supposedly sorted hash - using Tie::IxHash.
But, why my code below give:
1-1:80 2-4:72
instead of:
1-2:2196 2-2:180
The reorder method I am using seems to be correct, has splicing part too. But why?
Thanks,
Edward

%hash = ( '1-1' => 80, '1-4' => 26, '4-4' => 3, '2-2' => 180, '2-4' => 72, '1-2' => 2196 ); $N =2; %hash = seq_score($N,%hash); printhash(%hash); #---sub----- sub seq_score{ use Tie::IxHash; tie my %spair, "Tie::IxHash"; #modified ($N,%spair) = @_; my %topN_pair = (); my %seq =(); my $t = Tie::IxHash->new(%spair); #set new class $t->Reorder( reverse $t->SortByValue()->Keys() );#desc sort by value %topN_pair = $t->Splice(0, $N); return %topN_pair; } sub printhash { my %hash = @_; foreach my $key (sort{$hash{$b}<=>$hash{$a}} keys %hash ) { next if ($hash{$key} <=1); print $key, ":", $hash{$key}, "\n" } }

Replies are listed 'Best First'.
Re: Splicing and Sorting problem with Tie::IxHash
by Limbic~Region (Chancellor) on Oct 06, 2004 at 12:56 UTC
    ewijaya,
    Tie::IxHash's main focus is not sorting hashes, but preserving order. It does provide a couple of limited methods to sort the hash (by keys and values), but does not provide an interface to do a numerical sort. You likely want to use Tie::Hash::Sorted for this. See below:
    #!/usr/bin/perl use strict; use warnings; use Tie::Hash::Sorted; my $sort = sub { my $hash = shift; [ sort {$hash->{$b} <=> $hash->{$a}} keys %$hash ]; }; tie my %hash, 'Tie::Hash::Sorted', 'Sort_Routine' => $sort; %hash = ( '1-1' => 80, '1-4' => 26, '4-4' => 3, '2-2' => 180, '2-4' => 72, '1-2' => 2196 ); Print_Top(2, \%hash); sub Print_Top { my ($max, $hash) = @_; for ( keys %$hash ) { return if ! $max--; print "$_ : $hash->{$_}\n"; } }

    Cheers - L~R

Re: Splicing and Sorting problem with Tie::IxHash
by si_lence (Deacon) on Oct 06, 2004 at 07:41 UTC
    Hello, I don't see excatly what you are trying to do (still morning here ...).
    But to print out just the top two keys of your hash based on the value
    I would use something like this:
    use warnings; use strict; my %hash = ( '1-1' => 80, '1-4' => 26, '4-4' => 3, '2-2' => 180, '2-4' => 72, '1-2' => 2196 ); my $count=1; foreach my $key (sort{$hash{$b}<=>$hash{$a}} keys %hash ) { print "$key : $hash{$key}\n" ; last if $count++ > 1; }

    hth, si_lence
      Hi,
      Thanks for the reply.

      But,I need top-'N' values,
      and still keep it as a hash.
        OK. In this case I think you can make it without the tied hash.
        You will have to sort the output in printhash (as you did)
        to get them in descending order.
        si_lence
        %hash = ( '1-1' => 80, '1-4' => 26, '4-4' => 3, '2-2' => 180, '2-4' => 72, '1-2' => 2196 ); $N =2; %hash = seq_score($N,%hash); printhash(%hash); #---sub----- sub seq_score{ my %spair; ($N,%spair) = @_; my %topN_pair = (); my %seq =(); my $count=1; foreach my $key (sort{$spair{$b}<=>$spair{$a}} keys %spair ) { $topN_pair{$key}=$spair{$key}; last if $count++ >= $N; } return %topN_pair; } sub printhash { my %hash = @_; foreach my $key (sort{$hash{$b}<=>$hash{$a}} keys %hash ) { print $key, ":", $hash{$key}, "\n" } }