title: unique, count and sum values in perl
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/7916855/how-can-i-output-unique-count-and-sum-using-perl">How can I output unique, count and sum using perl</a>  </p>
  </blockquote>
  
  
  
  <p>How can I get unique, count and sum values in perl? My code is as follows:</p>
  
  <pre><code>   while (&lt;$input&gt;) {
                 chomp;
                 my($f1,$f2,$f3,$f4,$f5,$f6,$f7,$f8,$f9,$f10,$f11,$f12,$f13,$f14,$f15,$f16,$f17,$f18,$f19,$f20,$f21,$f22,$f23,$f24,$f25) = split(/\|/);
               $f24  = " " if !defined($f24);
  
               push @ff4, qw($f4); # VEN 10/19/11
  
               push @fff4, $f4, $f16, $f7; # VEN 10/28/11
               ..... ....... ...... ........ ......
               ..... ....... ...... ....... .......
      my %count;
      map { $count{$_} ++ } @array;
      my@count = map { "$_ ==========&gt; ${count{$_}}\n"} sort keys (%count);
  
      #print $output2 sprintf("@count\n");
  
  
      my %h;
      my @el;
      while (&lt;@array&gt;)
                      {
                        $h{$el[0]}{count}++;
                        $h{ $el[0]}{sum} += $el[2];
                      }
  
      print $output2 %h;
  </code></pre>
  
  <p>I am getting output like this</p>
  
  <pre><code>       08/2009 ====&gt; 2030
         08/2010 ====&gt; 2300
         09/2010 =====&gt; 1500
  </code></pre>
  
  <p>But I have to get it like this:</p>
  
  <pre><code>      08/2009 ====&gt; 2
        08/2010 ====&gt; 3
        09/2010 =====&gt; 5
  </code></pre>
  
  <p>I am using Perl on Solaris</p>