Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8926181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:54:20+00:00 2026-06-15T07:54:20+00:00

I have a hash that I sorted by values greatest to least. How would

  • 0

I have a hash that I sorted by values greatest to least. How would I go about getting the top 5? There was a post on here that talked about getting only one value.

What is the easiest way to get a key with the highest value from a hash in Perl?

I understand that so would lets say getting those values add them to an array and delete the element in the hash and then do the process again?

Seems like there should be an easier way to do this then that though.

My hash is called %words.

Edited Took out code as the question answered without really needing it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:54:22+00:00Added an answer on June 15, 2026 at 7:54 am

    Your question is how to get the five highest values from your hash. You have this code:

    my @keys = sort {
        $words{$b} <=> $words{$a}
        or
        "\L$a" cmp "\L$b"
    } keys %words;
    

    Where you have your sorted hash keys. Take the five top keys from there?

    my @highest = splice @keys, 0, 5;  # also deletes the keys from the array
    my @highest = @keys[0..4];         # non-destructive solution
    

    Also some comments on your code:

    open( my $filehandle0, '<', $file0 ) || die "Could not open $file0\n";
    

    It is a good idea to include the error message $! in your die statement to get valuable information for why the open failed.

    for (@words) {
        s/[\,|\.|\!|\?|\:|\;|\"]//g;
    }
    

    Like I said in the comment, you do not need to escape characters or use alternations in a character class bracket. Use either:

    s/[,.!?:;"]//g for @words;   #or
    tr/,.!?:;"//d  for @words;
    

    This next part is a bit odd.

    my @stopwords;
    while ( my $line = <$filehandle1> ) {
        chomp $line;
        my @linearray = split( " ", $line );
        push( @stopwords, @linearray );
    }
    for my $w ( my @stopwords ) {
        s/\b\Q$w\E\B//ig;
    }
    

    You read in the stopwords from a file… and then you delete the stopwords from $_? Are you even using $_ at this point? Moreover, you are redeclaring the @stopwords array in the loop header, which will effectively mean your new array will be empty, and your loop will never run. This error is silent, it seems, so you might never notice.

    my %words = %words_count;
    

    Here you make a copy of %words_count, which seems to be redundant, since you never use it again. If you have a big hash, this can decrease performance.

    my $key_count = 0;
    $key_count = keys %words;
    

    This can be done in one line: my $key_count = keys %words. More readable, in my opinion.

    $value_count = $words{$key} + $value_count;
    

    Can also be abbreviated with the += operator: $value_cont += $words{$key}

    It is very good that you use strict and warnings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume that I have a complex hash reference $hash_ref , and I would
I have a hash that contains a number of keys/values that need sorting by
I have a hash table that contains values of a^j. j is the key
I have a hash that I'm getting from using JSON.parse. I am inserting into
I have @hash that looks like this: [1, {:clid=>1, :nvz=>4, :tip=>IP, :name=>Mark, :record=>some text}]
I have a hash that looks like this h1 = {4c09a0da6071a593f051de32=>[4c09a0da6071a593f051de32, Cafe Bistro, 37.78458803130115,
I am currently learning Perl. I have Perl hash that contains references to hashes
I have a counter hash that I am trying to sort by count. The
I have a ruby hash that I'd like to render using RABL . The
I have an object of type Hash that I want to loop over via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.