Hi Hena,
I am attempting to generalize the problem above, such that now it can count support based on "string mismatch" paramaters. The code below is inspired by your posting above, and it already gives the correct answer. I know that the my code is a "idiot" solution to your ingenious approach above. I don't understand much of your code (especially that one-liner with many '&&'). I wonder how would you make my code compact like yours? I really want to learn to write my code briefly.
#!/usr/bin/perl -w use strict; use Data::Dumper; my $hash_mismatch = { # no match because hd($query[0],$hash_mismatch->{'S1'}[0]) > $d # even though hd($query[1],$hash_mismatch->{'$2'}[1]) < $d 'S1' => [ 'GGAA', 'GGGG', 'TTTT' ], # match because hd($query[0],$hash_mismatch->{'S2'}[0]) = $d # and hd($query[1],$hash_mismatch->{'$2'}[2]) < $d 'S2' => [ 'GGGA', 'GTTT', 'GGGG' ], # match because hd($query[0],$hash_mismatch->{'S3'}[0]) = $d # and hd($query[1],$hash_mismatch->{'$3'}[1]) = $d 'S3' => [ 'GGGA', 'GCGG', 'GTTT' ], # match because hd($query[0],$hash_mismatch->{'S4'}[0]) < $d 'S4' => [ 'GGGG', 'AAAA', 'GGGG' ] }; my $d = 0; # mismatch/hamming distance; # With $d = 1; the answer : support: 3 (from 'S2,S3,S4); # With $d = 0; the asnswer is : support: 1 (from 'S4') my @query = ('GGGG','GGGG'); my $sup = count_support_mismatch($hash_mismatch,\@query,$d); print "Support: $sup\n"; #------------------Subs----------- sub count_support_mismatch { my ($hashref,$arref,$d) = @_; my $counter = 0; foreach my $key ( keys %{$hashref} ) { my $ar = $hashref->{$key}; my @match_list; foreach ( @{$ar} ) { foreach my $q ( @$arref ) { my $dist = hd($_,$q); if ( $dist <= $d ) { #print "$q - $_\n"; push @match_list, $_; } shift @{$ar}; } } if ( @match_list ) { $counter++; } } return $counter; } sub hd { #String length is assumed to be equal my ($a,$b) = @_; my $len = length ($a); my $num_mismatch = 0; for (my $i=0; $i<$len; $i++) { ++$num_mismatch if substr($a, $i, 1) ne substr($b, $i, 1); } return $num_mismatch; }
Regards,
Edward

In reply to Re^2: Counting Array in an Array of HoA by monkfan
in thread Counting Array in an Array of HoA by monkfan

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.