Hi,
Given the input of array and max string mismatch as params, I want to count its element into HoH format. I thought I get it right, but somehow it's not quite there. How can I modify the "read_trans_array_mismatch" subroutine in the code here:
#!/usr/bin/perl -w use strict; use Data::Dumper; my @to_proc = ( 'I1 TTAT', 'I1 TTTT', 'I1 TAGT', 'I2 TTAT', 'I3 TAGT', ); my $d = 1; my %transaction_map_mismatch; read_trans_array_mismatch(\%transaction_map_mismatch, \@to_proc,$d); print Dumper \%transaction_map_mismatch; #--------Sub------------ sub read_trans_array_mismatch { my $transaction_map_ref = shift; my $transaction_array = shift; my $d = shift; for ( my $i = 0; $i< @{$transaction_array}; $i++ ) { my @data = split(/\s/,$transaction_array->[$i]); my ($tid, $item) = @data; for ( my $j = 0 ; $j < @{$transaction_array} ;$j++ ) { my ($tid2, $item2) = split(/\s/,$transaction_array->[$j]); if ( hd($item,$item2) <= $d ) { $$transaction_map_ref{$item}{$tid}++; last; } } } } sub hd { #Hamming Distance of two strings #String length is assumed to be equal # Following djohntson advice, changed var declaration # from: my ($a, $b) my ($k,$l) = @_; my $len = length ($k); my $num_mismatch = 0; for (my $i=0; $i<$len; $i++) { ++$num_mismatch if substr($k, $i, 1) ne substr($l, $i, 1); } return $num_mismatch; }
Such that with $d = 1 it gives:
__END__ $VAR1 = { 'TAGT' => { 'I1' => 1, 'I3' => 1 }, 'TTTT' => { 'I1' => 2, #From TTAT and TTT in I1, #because their HD <= $d 'I2' => 1, }, 'TTAT' => { 'I2' => 1, 'I1' => 2 # also From TTAT and TTT in I1 } };
Currently with $d = 1 it gives wrongly this result:
# Which is the same as $d = 0 $VAR1 = { 'TAGT' => { 'I3' => 1, 'I1' => 1 }, 'TTTT' => { 'I1' => 1 }, 'TTAT' => { 'I2' => 1, 'I1' => 1 } };
Regards,
Edward

In reply to Element Count from Array to HoH 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.