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

hi all, i have a hash containg 60 key value pairs which are sorted. from that i want to print top 5 how can i do it. Thanks
  • Comment on how to print top 5 elements from a hash

Replies are listed 'Best First'.
Re: how to print top 5 elements from a hash
by Fletch (Bishop) on Jun 01, 2009 at 15:43 UTC

    You're mistaken as hashes are (by nature) unsorted. You may very well have a sorted list of keys in which case you can use a slice (my @first_five = @sorted_keys[0..4]).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: how to print top 5 elements from a hash
by lostjimmy (Chaplain) on Jun 01, 2009 at 15:46 UTC

    First of all, I don't know what you mean by "sorted". A hash is not going to be sorted unless it's tied, like Tie::IxHash.

    What you are trying to do is fairly easy. What have you tried so far? We'd love to put in the effort to help you if you would be willing to put in some effort.

    To get you started, I would approach this with the following steps:

    1. Put sorted keys into @keys 2. Loop over the first five elements of @keys (many ways to achieve th +is) 3. Print those five records in the loop: print $hash{$key}

Re: how to print top 5 elements from a hash
by ikegami (Patriarch) on Jun 01, 2009 at 15:55 UTC
    for my $i ( 0 .. 4 ) { my $key = $sorted_keys[$i]; print "$hash{$key}\n"; }

    or

    for my $key ( @sorted_keys[ 0 .. 4 ] ) { print "$hash{$key}\n"; }

    or

    my @values = @hash{ @sorted_keys[ 0 .. 4 ] };
Re: how to print top 5 elements from a hash
by bichonfrise74 (Vicar) on Jun 01, 2009 at 21:36 UTC
    How about this?
    #!/usr/bin/perl use strict; my %hash = ( 'a' => '1', 'b' => '2', 'c' => '3', 'f' => '6', 'e' => '5', 'd' => '4' ); my $count = 0; foreach my $i ( sort keys %hash ) { print "$i ==> $hash{$i}\n" if ($count <= 4); $count++; }
      This is good, but since (sort keys %hash) is a list, we can just take a list slice:
      my %hash = ( 'b' => '2', 'a' => '1', 'c' => '3', 'f' => '6', 'e' => '5', 'd' => '4' ); foreach my $key ( (sort keys %hash)[0..4] ) { print "$key ==> $hash{$key}\n" } #prints.. #a ==> 1 #b ==> 2 #c ==> 3 #d ==> 4 #e ==> 5
Re: how to print top 5 elements from a hash
by vinoth.ree (Monsignor) on Jun 02, 2009 at 03:54 UTC

    Hash will not be sorted by itself. Do sort based on key and take top 5 elements and print the hash.

    use strict; use warnings; my %hash=(key6=>'value6',key2=>'value2',key1=>'value1',key3=>'value3', +key4=>'value4',key5=>'value5'); my @arr=sort keys %hash; print map{$_.'=>'.$hash{$_}."\n"}@arr[0..4];
    Vinoth,G
Re: how to print top 5 elements from a hash
by arc_of_descent (Hermit) on Jun 01, 2009 at 16:07 UTC
    You might need to read up on sort, if you need to sort your hash keys first.

    --
    Rohan
Re: how to print top 5 elements from a hash
by lloder174 (Novice) on Jun 01, 2009 at 20:45 UTC
    my $counter = 0; foreach my $key ( sort keys %hashname ) { $counter++; print " key ==> value number $counter : " . "$key ==> $hashname{$key} \n"; if ( $counter == 5 ) { last; } }