title: creating a hash with regex matches in perl
body: <p>Lets say i have a file like below:</p>
  
  <p>And i want to store all the decimal numbers in a hash.</p>
  
  <pre><code>hello world 10 20
  world 10 10 10 10 hello 20
  hello 30 20 10 world 10
  </code></pre>
  
  <p>i was looking at <a href="http://theunixshell.blogspot.com/2013/01/capturing-all-regex-matches-into-array.html" rel="nofollow">this</a></p>
  
  <p>and this worked fine:</p>
  
  <pre><code>&gt; perl -lne 'push @a,/\d+/g;END{print "@a"}' temp
  10 20 10 10 10 10 20 30 20 10 10
  </code></pre>
  
  <p>Then what i need was to count number of occurrences of each regex.</p>
  
  <p>for this i think it would be better to store all the matches in a hash and assign an incrementing value for each and every key.</p>
  
  <p>so i tried :</p>
  
  <pre><code>perl -lne '$a{$1}++ for ($_=~/(\d+)/g);END{foreach(keys %a){print "$_.$a{$_}"}}' temp
  </code></pre>
  
  <p>which gives me an output of:</p>
  
  <pre><code>&gt; perl -lne '$a{$1}++ for ($_=~/(\d+)/g);END{foreach(keys %a){print "$_.$a{$_}"}}' temp
  10.4
  20.7
  </code></pre>
  
  <p>Can anybody correct me whereever i was wrong?</p>
  
  <p>the output i expect is:</p>
  
  <pre><code>10.7
  20.3
  30.1
  </code></pre>
  
  <p>although i can do this in awk,<strong>i would like to do it only in perl</strong></p>
  
  <p>Also order of the output is not a concern for me.</p>