To your error msg, "@match_count = sort{$b ->{max} <=> $a->{max}} @match_count; #CODE", "Use of uninitialized value in numeric comparison (<=>)".

So looks like @match_count is an array of hash references. One of those hash references has an undefined value for the key "max". Why not add some debugging code to print each $ref->{max} of refs in @match_count before the sort to see what it going on? Or simply use Data::Dumper; print Dumper \@match_count;

One Perl feature that looks like it would be useful to you is the //= operator. This can be used to set a variable to some default value if it is undefined. $var //= 1; Would set $var to 1 if it is undefined. If it is 0 or any other value, it is unchanged. I am still looking at your code, but I think you were trying to do something like that earlier but with the ||= operator? Not sure.

Update:
I was looking further as to how $ref->{max} could wind up being undef. Something may be going wrong in the count hash. I do admit to being a bit puzzled by the code absent a textual explanation context. However, consider the following:

if($k_nuc eq $i_nuc){ $count->{$rel} ||= 0; $count->{$rel} += 1; }else{ $count->{$rel} ||= 0; $count->{$rel} += 0; } === possible change could be: ===== $count->{$rel} //= 0; ### probably not needed, see below if($k_nuc eq $i_nuc){ $count->{$rel}++; }
This makes sure that $count->{$rel} is created and set to zero if it isn't already defined. The //= operator will not affect the value if it already is defined to be something. Increment it if the condition is true. I don't see a need for ||= 0 and for sure not += 0.

It is a bit odd to even have to do this at all. $count->{$rel}++; will work even if the value doesn't even exist yet. This is actually a very common Perl idiom. I don't see anywhere where you make use of the zero values, so why even create them in the first place? If you need them, then fine. Otherwise the code can be simplified to just make hash entries if the value is 1 or greater. The //= operator was added I think in Perl 5.10 which was a long time ago.


In reply to Re: Help with function, count matches and store in hash & retrieve sorted by Marshall
in thread 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.