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:
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.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}++; }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |