in reply to Count values in a hash

You need to dereference the array.
use warnings; use strict; my %hash = ( 'A2M' => [ 'C972Y', 'A423W' ], 'A4GALT' => [ 'W261X', 'P251L', 'P251S', 'A219V' ] ); for my $k (sort keys %hash) { print "Current key is\t" . $k . "\n"; my $totalcount = @{ $hash{$k} }; my $xcount = grep { /X/ } @{ $hash{$k} }; print $k . "\t" . $totalcount . "\t" . $xcount . "\n"; } __END__ Current key is A2M A2M 2 0 Current key is A4GALT A4GALT 4 1
See also: grep and perldsc

Replies are listed 'Best First'.
Re^2: Count values in a hash
by sophix (Sexton) on Jan 11, 2011 at 18:01 UTC

    Thank you very much, toolic.

    Does @{ $hash{$k} } work as a reference? What does  @{} do?

        Ah, okay. Then this explains why the array did not work in my case. I need to dereference it as to work on it. Thanks for the links, I'll check them out now. I guess a good knowledge of using references in Perl would help me solve many of my problems.