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

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.

Replies are listed 'Best First'.
Re^2: Help with function, count matches and store in hash & retrieve sorted
by Pathogenomix (Novice) on Jul 03, 2017 at 00:48 UTC

    hello all and thanks for the help,so far. I have made sure that a query sequence is being provided. By running the debug statement suggested above I have found that I have undef max values. How can I get around this? Thank you very much everyone!

    $VAR1 = [ { 'bar' => 'TATCCTCT', 'max' => undef }, { 'bar' => 'CTCTCTAT', 'max' => undef }, { 'bar' => 'AGCGTAGC', 'max' => undef }, { 'bar' => 'AGAGTAGA', 'max' => undef }, { 'max' => undef, 'bar' => 'CTAAGCCT' }, { 'bar' => 'CATGCCTA', 'max' => undef }, { 'bar' => 'TTCTGCCT', 'max' => undef }, { 'max' => undef, 'bar' => 'GCTCAGGA' }, { 'max' => undef, 'bar' => 'TCGCCTTA' }, { 'max' => undef, 'bar' => 'CCTCTCTG' }, { 'bar' => 'TAGATCGC', 'max' => undef }, { 'max' => undef, 'bar' => 'ACTGCATA' }, { 'bar' => 'GCGTAAGA', 'max' => undef }, { 'bar' => 'CTAGTACG', 'max' => undef }, { 'bar' => 'AAGGAGTA', 'max' => undef }, { 'max' => undef, 'bar' => 'GTAGAGAG' } ]; DEBUG: [TATCCTCT]=>[11] DEBUG: [CTCTCTAT]=>[10] DEBUG: [AGCGTAGC]=>[5] DEBUG: [AGAGTAGA]=>[12] DEBUG: [CTAAGCCT]=>[16] DEBUG: [CATGCCTA]=>[6] DEBUG: [TTCTGCCT]=>[3] DEBUG: [GCTCAGGA]=>[4] DEBUG: [TCGCCTTA]=>[1] DEBUG: [CCTCTCTG]=>[8] DEBUG: [TAGATCGC]=>[9] DEBUG: [ACTGCATA]=>[14] DEBUG: [GCGTAAGA]=>[13] DEBUG: [CTAGTACG]=>[2] DEBUG: [AAGGAGTA]=>[15] DEBUG: [GTAGAGAG]=>[7]
      I updated my post Re: Help with function, count matches and store in hash & retrieve sorted, is that helpful to you?

      Well, after looking yet again, I am still not sure. Can you give use the exact inputs that you are using so that we can run and exactly replicate your error with your data?

      I am a bit puzzled about how you can get an undef value for $max_count? Which is assigned to the key "max". Of course:

      my $element; $element->{bar} = $key; $element->{max} = $max_count; $element->{max} //= 0; #### adding this prevents undef push @match_count, $element;
      I guess I have the Sunday brain cramp.