title: Adding up the values in a hash (Perl)
body: <p>I want to add up the values from a hash that I have created.  </p>
  
  <pre><code>my $value_count;
  foreach my $key (@keys) {
      $value_count = sum($words{key}, $value_count);
  }
  </code></pre>
  
  <p>However when I run this I get </p>
  
  <pre><code>Undefined subroutine &amp;main::sum called at C:/Users/Clayton/workspace/main/Main.pl line 54, &lt;$filehandle1&gt; line 174.
  </code></pre>
  
  <p>I'm not really sure where I am going wrong here. I'm new to Perl.  </p>
  
  <p><strong>EDIT:</strong>  I tried using just + operator but I get the error</p>
  
  <pre><code>Use of uninitialized value in addition (+) at C:/Users/Clayton/workspace/main/Main.pl line 54, &lt;$filehandle1&gt; line 174.
  </code></pre>
  
  <p>Pretty much my hash is like
      Key    Value
      cat    2
      dog    4
      rat    1</p>
  
  <p>So I'm trying to add up all the values so I can take an average.  </p>
  
  <p><strong>EDIT 2:</strong>  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.  </p>