Hi Monks

I am having a file which as follows:

2_SecVal 4 1 1 0 0 0 1 0 0 1 0 1_SecVal 4 1 0 1 0 1 0 0 0 1 0 2_FirVal 4 1 0 1 0 1 1 0 0 0 0 3_FirVal 4 0 0 1 0 1 1 0 0 1 0 3_SecVal 4 0 0 1 0 1 1 0 0 1 0 2_FirVal 5 1 0 1 0 0 0 1 1 0 1 4_SecVal 5 1 0 1 0 1 1 0 0 1 0 5_SecVal 5 1 0 1 0 1 1 0 0 1 0 7_FirVal 5 1 0 1 0 1 1 0 0 1 0 4_FirVal 5 1 0 1 0 1 1 0 0 1 0 5_FirVal 5 1 1 0 0 0 0 1 1 0 1 6_FirVal 5 1 0 1 0 1 1 0 0 1 0 6_SecVal 5 1 0 1 0 0 0 1 1 0 1 7_SecVal 5 1 1 0 0 0 0 1 1 0 1

I am reading the file into hash of scalar references with first column as first key and second column as the second key. I am trying to match the values which are common in the entire hash and I am counting it to find the number of common value in the hash. I am using "undef" to remove the pattern which is matching (while iterating -- which I think is not good way of programing, so any other tips for the same) and therefore would like to know the total number of them. I am little skeptical and therefore would like to know whether it is working or not. My code is as follows:

while(<$IN>){ chomp; my ($id, $sum, $haploStr) = split ('\t',$_); push @{$data{$id}{$sum}}, \$haploStr; } my @keys = keys %data; my $j = 1; for my $i ( 0 .. $#keys ) { my $key1 = $keys[$i]; my $count = 1; for my $sum1 ( %{ $data{$key1} } ) { for my $str1 (@{$data{$key1}{$sum1}}){ for my $key2 ( @keys[ $i + 1 .. $#keys ] ) { for my $sum2 ( %{ $data{$key2} } ) { for my $str2(@{$data{$key2}{$sum2}}){ if ($$str1 eq $$str2){ #print "MATCH: $$str1 \t $$str2\n "; $count++; undef @{$data{$key2}{$sum2}}; } } } } } } print "H$j\tMatching Pattern as $key1\t $count\n"; $j++; }

Also, it is taking more time than expected therefore would like to known your comments. Thanks.

Update: I have the change from array to scalar references. I am sorry for that

In reply to Extract the common values in hash of array with double keys by snape

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.