sophix has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Count values in a hash
by toolic (Bishop) on Jan 11, 2011 at 17:49 UTC | |
by sophix (Sexton) on Jan 11, 2011 at 18:01 UTC | |
by toolic (Bishop) on Jan 11, 2011 at 18:05 UTC | |
by sophix (Sexton) on Jan 11, 2011 at 18:11 UTC | |
|
Re: Count values in a hash
by rir (Vicar) on Jan 11, 2011 at 18:29 UTC | |
|
Re: Count values in a hash
by jwkrahn (Abbot) on Jan 11, 2011 at 21:42 UTC | |
|
Re: Count values in a hash
by ack (Deacon) on Jan 11, 2011 at 21:53 UTC | |
|
Re: Count values in a hash
by suhailck (Friar) on Jan 12, 2011 at 07:55 UTC |