Head the other responders' replies. Failure to de-reference your hash and array are the source of the difficulty as the other responders noted.

Here is a quick, short set of code that does what I believe your looking to do. I include it just so that you can see one way to do it. Of course, as in all of Perl, TMTODI.

Cheers.

#!/usr/bin/perl use strict; use warnings; my $VAR1 = { 'A2M' => [ 'C972Y', 'A423W' ], 'A4GALT' => [ 'W261X', 'P251L', 'P251S', 'A219V' ] }; my $numKeys = scalar(keys %{$VAR1}); print "gene total x\n"; foreach my $key (keys %{$VAR1}){ my @values = @{$VAR1->{$key}}; my $numValues = scalar(@values); my $numXs = 0; $numXs += ($_ =~ /X/g)?1:0 foreach(@values); print "$key $numValues $numXs\n"; } exit(0);

The resulting output is:

gene total x A2M 2 0 A4GALT 4 1

Which is what I believe you are after.

The only difficulty is that the pattern match, $_ =~ /X/g is not very general...it wouldn't, for instance, see multiple occurances of 'X' in a value (you, the OP, didn't specify what to do if such an occurance presented itself) nor would it distinguish upper case 'X' from a lower case 'x' (you didn't specify if that mattered). But I figured either you'd already know how to handle those situations or they would be good explorations of Perl for you.

Also note that I just initialize the hash with the example that you gave rather than reading it in from a file because I felt that it would just obscure the main body.

ack Albuquerque, NM

In reply to Re: Count values in a hash by ack
in thread Count values in a hash by sophix

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.