Hi,
Thanks so much for the reply:
Hashes by definition don't have a (usable) sequence or order. Therefore
your request for the top n elements of a hash doesn't make any sense.
Sorry for not being clear before.
I should have sorted the hash according to it's value
and then pick top-N from it.
It's difficult for me to change the current
data-structure of my code, as it's inherently designed
for hash.
Moreover, I found about Tie::IxHash module.
Which is suppose to "impose" order in the hash.
However since I am new in using Method through external
module, there are 2 issues I fail to deal with.
Your valuable suggestion is important to me.
Here is my code:
__BEGIN__
use strict;
use warnings;
use Tie::IxHash;
my $N = 2;
my %hash = {
#1) Must sort by value,then 2) Select Top N after sorting it.
'1-1' => 3,
'2-3' => 2,
'2-2' => 1,
'1-2' => 6,
'1-3' => 3
};
my $t = Tie::IxHash->new(%hash);
$t->SortByValue; #sorting it, is this correct?
my @topN = $t->Splice(0, $N); #splicing it
__END__
My problem is:
1. Is the way I invoke the Sorted new hash is correct?,
How can I view it? I tried: print "%$hash\n"; # doesn't seem to work
2. The "spliced" array is in the form of array,
Is there any efficient way/method to recover it into Hash?
Thanks and hope to hear from you again,
Regards
Edward WIJAYA
SINGAPORE