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 8931519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:07:51+00:00 2026-06-15T09:07:51+00:00

I want to add up the values from a hash that I have created.

  • 0

I want to add up the values from a hash that I have created.

my $value_count;
foreach my $key (@keys) {
    $value_count = sum($words{key}, $value_count);
}

However when I run this I get

Undefined subroutine &main::sum called at C:/Users/Clayton/workspace/main/Main.pl line 54, <$filehandle1> line 174.

I’m not really sure where I am going wrong here. I’m new to Perl.

EDIT: I tried using just + operator but I get the error

Use of uninitialized value in addition (+) at C:/Users/Clayton/workspace/main/Main.pl line 54, <$filehandle1> line 174.

Pretty much my hash is like
Key Value
cat 2
dog 4
rat 1

So I’m trying to add up all the values so I can take an average.

EDIT 2: The actual fix is in the comments I needed to make my $value_count = 0. That fixed everything. Thank you all. I think this is an important issue to be resolved and I think it could be useful for someone else so I’m going to leave 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-15T09:07:53+00:00Added an answer on June 15, 2026 at 9:07 am

    To use the sum function, you need the List::Util package. But that isn’t needed in this case, as you can use the + operator:

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

    In fact, you could use sum and avoid the loop. This is the solution you should use:

    use List::Util 'sum';
    my $value_count = sum values %words;
    

    The values function returns the values of a hash as a list, and sum sums that list. If you don’t want to sum over all keys, use a hash slice:

    use List::Util 'sum';
    my $value_count = sum @words{@keys};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of string values that I want add to a hashtable
I have a dictionary that I want to add all the values to an
I want to add and fetch the value from dynamically created textfield with tag
I have an array that contains values from a cookie, if one of those
I want to add some values in arraylist but at random index. Is this
I'm developing web-application, in this I want to add all months values on the
I want to insert row for each day to add new values, and I
I have a dropdownlist.I want to add selected value of it to a hyperlink
I want to load the key value pairs from a class, or id, in
I want to show continuous integer values from a certain min value to max

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.