Hello all, I am trying to use a function that essentially counts matches of a string with a list of strings that have indices. (i.e. 1,"ATAT"\n 2,"GCGC" \n etc.). The code takes two inputs, one is a hash of sequence and index numbers that looks like so

indexfile' => { 'GCTCAGGA' => '4', 'AGCGTAGC' => '5', 'TTCTGCCT' => '3', 'CATGCCTA' => '6', 'TCGCCTTA' => '1', 'GTAGAGAG' => '7', 'GCGTAAGA' => '13', 'TAGATCGC' => '9', 'CTAGTACG' => '2', 'CTAAGCCT' => '16', 'CTCTCTAT' => '10', 'ACTGCATA' => '14', 'AGAGTAGA' => '12', 'TATCCTCT' => '11', 'AAGGAGTA' => '15', 'CCTCTCTG' => '8' },

and the other file is a query sequence string. I want to find the best match between them. The code fails at the line I will comment on, and I would really appreciate any insight into why.

sub match_query(){ my $indexfile = shift; my $queryseq = shift; my @match_count; for my $key (keys %$indexfile){ my $count; for(my $k=0; $k<length($key)-1; $k++){ my $k_nuc = substr $key,$k,2; for(my $i=0; $i<length($queryseq)-1; $i++){ my $i_nuc = substr $queryseq,$i,2; my $rel = $k-$i; if($k_nuc eq $i_nuc){ $count->{$rel} ||= 0; $count->{$rel} += 1; }else{ $count->{$rel} ||= 0; $count->{$rel} += 0; } } } my @array = sort{$b <=> $a} values %$count; my $max_count = shift @array; my $element; $element->{bar} = $key; $element->{max} = $max_count; push @match_count, $element; } my $result; @match_count = sort{$b ->{max} <=> $a->{max}} @match_count; #CODE +#FAILS HERE DOESNT LIKE ->{max} if($match_count[0]->{max} == $match_count[1]->{max}){ $result = 0; }elsif($match_count[0]->{max}>5){ my $called_bar = $match_count[0]->{bar}; $result = $indexfile->{$called_bar}; }else{ $result = 0; } return $result }

If anyone could explain to my why the code fails I would really appreciate it, the error message I recieve looks like this:

Use of uninitialized value in numeric comparison (<=>) at /Users/XXXXX +X/Desktop/test.pl line 602.

In reply to Help with function, count matches and store in hash & retrieve sorted by Pathogenomix

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.