my $totalcount = 0; my $xcount = 0; foreach my $k (sort keys %hash) { print "Current key is\t" . $k . "\n"; my @values = $hash{$k}; foreach $i (@values){ $xcount++ if $i =~ /(X)/; $totalcount++; } print $k . "\t" . $totalcount . "\t" . $xcount . "\n";