Hi all,
I have a hash of the following format
$VAR1 = { 'A2M' => [ 'C972Y', 'A423W' ], 'A4GALT' => [ 'W261X', 'P251L', 'P251S', 'A219V' ] };
What I would like to do is to count the values for each key and also to note the number of values that contain an X. Thus, I would like to have a table like this:
gene total x A2M 2 0 A4GALT 4 1
The code I used to load the file into the hash is
my %hash; while(<INFILE>) { chomp; my $line = $_; my ($key, @val) = split /\t/, $line, 2; push @{ $hash{$key} }, @val; }
And the script that I though would work for the second part but did not is as follows:
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";
I would appreciate any help. when I try to check whether the value array works properly, I noticed that it does not, however, I could not pinpoint what is exactly wrong with the values array.
Thank you.
In reply to Count values in a hash by sophix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |