monkfan has asked for the wisdom of the Perl Monks concerning the following question:
instead of:1-1:80 2-4:72
The reorder method I am using seems to be correct, has splicing part too. But why?1-2:2196 2-2:180
%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 | |
|
Re: Splicing and Sorting problem with Tie::IxHash
by si_lence (Deacon) on Oct 06, 2004 at 07:41 UTC | |
by monkfan (Curate) on Oct 06, 2004 at 07:57 UTC | |
by si_lence (Deacon) on Oct 06, 2004 at 10:10 UTC |