You might want to use the bin number as the key of the hash and add the count to it whenever you encounter it. It could look like this:
use strict; use warnings; my %hash; my $data = "BIN COUNT 1 20 2 0 2 1 3 0 3 0 3 1 7 0 8 2 9 1 9 2 10 3"; $hash{$1} += $2 while $data =~ /(\d+) (\d+)/g; print "BIN COUNT "; print "$_ $hash{$_} " for sort {$a<=>$b} keys %hash;
Also, please use code tags in your post to make it more readible.
In reply to Re: merge duplicate values in hash
by hdb
in thread merge duplicate values in hash
by eric_newbie
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |