-2

I want to get the number of entries for each group, but it seems to be giving me the wrong answer. There are elements in each group that might be repeated. The results I'm getting don't make sense. What am I doing wrong?

my %hash;
while(<>)
{
    chomp($_);
    if(/(\d+)\t(\d+)/)
    {
        my $group = $1;
        my $element = $2;
        $hash{$group}{$element}=1;
    }
}

foreach my $curr(keys %hash)
{
    my $numElementsInCurr = keys %{$hash{$curr}};
    print "$curr\t$numElementsInCurr\n";
}
Jeff B
  • 29,943
  • 7
  • 61
  • 90
user1645240
  • 55
  • 1
  • 7
  • 1
    Are you sure your input is well formed? You could use `Data::Dumper` to examine `%hash` to see that it looks like what you expect. – zigdon Feb 27 '13 at 01:55
  • 1
    You should mention why you think it is giving you the wrong answer. Just posting code that does what it is supposed to isn't going to help at all. Such as printing Dumper output for `%hash` and showing the output of your loop. – TLP Feb 27 '13 at 02:01
  • 1
    You haven't quite demonstrated the problem. Your code wasn't quite runnable (which I fixed), but you haven't given data that causes the error to happen. It would also be nice to get the output you get for that data, and the output you expect. – ikegami Feb 27 '13 at 02:06

1 Answers1

0

If elements in a group can be repeated, and you need your count to include these duplicates, you will need to do something more like:

my %hash;
while(<>)
{
    chomp($_);
    if(/(\d+)\t(\d+)/)
    {
        my $group = $1;
        my $element = $2;
        ++$hash{$group}{$element};
    }
}

use List::Util 'sum';

foreach my $curr(keys %hash)
{
    my $numElementsInCurr = sum values %{$hash{$curr}};
    print "$curr\t$numElementsInCurr\n";
}
ysth
  • 96,171
  • 6
  • 121
  • 214