professa has asked for the wisdom of the Perl Monks concerning the following question:

Hi dear monks!

I'm having problems in using a regexp and hashed hashes. I have the following variables:

$code = reference to an array holding identifiers $hashhash = reference to a hash

The hash has the following form:

$hashhash = { 'identifier' => {'score' => 7, 'query_start' => 80, 'query_end' => 1292, 'subj_start' => 367, 'subj_end' => 1563 'e_value' => '5e-12', 'defline' => 'description', 'taxon' => 'Homo sapiens', }, 'identifier' => {...} }

My buggy code looks like this:

if ($hashhash->{$codes->[$x]}->{'defline'} =~ m/NP_/) { do_something; }

$x is cycled from 0 to scalar(@{$code}); and I'm searching for 'NP_' in the defline. The code crashes with:

Can't use string ("description") as an ARRAY ref while "strict refs" in use at /home/spitzem/wp/retr_standaloneblast_ +ABCGfam.pl line 589 (#1) (F) Only hard references are allowed by "strict refs". Symbolic references are disallowed. See perlref.

I don't see any where I use the description-string as an ARRAY-reference. The problem is that some cycles work flawlessly, but then it crashes and I don't see any difference or reason what could explain this behaviour. Any hints are welcome, I'm just at the edge of knocking my head on the table... ;-)

Cheers, Micha

Replies are listed 'Best First'.
Re: strict refs and REGEXP error
by cchampion (Curate) on Apr 30, 2002 at 09:35 UTC
    This is not real code. (Your hash can't have two hash items named 'identifier'.) From the snippet you submitted, I would say that the error is in $codes->[$x], but I couldn't be sure without seeing which values $codes is supposed to hold.

    You say that the problem is with "strict refs".
    Why don't you make a small script, with only the loop you want to check, and try it out with and without "use strict"? If it works without use strict, then I would like to see the code ;). If it doesn't, then you have a mistake in your $codes->[$x].
    cchampion
      No, it's no real code, 'identifier' was meant as a generic name for the hash keys.
      Making a small script isn't trivial since I gather the data to build the hash and the 'code'-array in several nested subroutines. I did a 'workaround' by now, which means I recoded stuff using more nested 'if'-constructs than in the stripped-down (but functional and error-producing) code below:
      for (my $y = 0; $y < $num; $y++) { for (my $x = $y+1; $x < $num; $x++) { if (($scores->[$y]->[$x] >= 0.96)) { if ($blast_par->{$cleancodes->[$x]}->{"taxon"} eq $blast_par->{$cleancodes->[$y]}->{"taxon"}) { if (($blast_par->{$cleancodes->[$y]}->{"defline"} =~ m/ +refseq/i) && ($blast_par->{$cleancodes->[$x]}->{"defline"} =~ m/ +refseq/i)) { if ($blast_par->{$cleancodes->[$y]}->{"defline"} =~ +m/NP_/) { # this line produces the error $to_keep = $y; $to_kill = $x; } elsif (($blast_par->{$cleancodes->[$y]}->{"defline"} +->[$y] !~ m/NP_/) && ($blast_par->{$cleancodes->[$x]}->{"defline"} +->[$x] =~ m/NP_/)) { $to_keep = $x; $to_kill = $y; } } &generic_sub($to_kill, $to_keep); } } } }
      I think the error has something to do with the 'if'-lines not meeting the "... =~ m/NP_/" requirement. I split up the if's in a more simple (but also more inelegant ;-) manner and it works now. From Data::Dumper()-analysis the array and the hash itself are valid.
      But anyway, I'd like to have a clear mind about what happened here to avoid such errors in future, so, if anyone's got an idea... ;-)

      Micha

Re: strict refs and REGEXP error
by Sifmole (Chaplain) on Apr 30, 2002 at 13:19 UTC
    I believe you might be victim to a misindentified line, or yourself misindentifying it. This code looks wrong to me:
    elsif (($blast_par->{$cleancodes->[$y]}->{"defline"}->[$y] !~ m/NP_/) +&& ($blast_par->{$cleancodes->[$x]}->{"defline"}->[$x] =~ m/NP_/)) {
    $blast_par->{$cleancodes->[$x]}->{"defline"} should point to "description", but you then add ->[$x] which attempts to dereference "description" as an array.
      Whoops!
      You're right, that '...->{"defline"}->[$y]' shouldn't be there, I didn't notice that. But I just wonder why Perl complains about a misused ARRAY-ref in the previous 'if'-statement and not in the faulty 'elsif'-line...?

      => I just tested the old subroutine with the buggy code and corrected it like you proposed and...it works.
      Very confusing that Perl reports an error a few lines up and not for the buggy line itself... ;-)
      Thanx for clarifying my daily Perl-nebulae a bit!! :-)

      Cheers, Micha

It's gotta be $codes
by RMGir (Prior) on Apr 30, 2002 at 10:27 UTC
    The only thing you're using as an array ref in that snippet is $codes.

    I'm guessing $codes gets set to "description" at some point, overwriting the array reference.

    Edit: Hmmm, looking at the additional info in your 2nd post, I take that back. That's just WIERD, since you just dereferenced the same array ref in the previous conditional without an error. Odd...
    --
    Mike