in reply to Why does this sub grab the whole hash

Alter your code as follows and see what comes out:

print $hashofseqs{someIDnr}, "\n"; $stringforsequence=$hashofseqs{someIDnr}; $reversedsequence=refcompl($stringforsequence); print $reversedsequence, "\n";

Dave

Replies are listed 'Best First'.
Re^2: Why does this sub grab the whole hash
by naturalsciences (Beadle) on Nov 11, 2011 at 18:04 UTC

    Done this thing a lot already :D

    print $hashofseqs{someIDnr}, "\n";

    gives me what I expect, a nice little sequence from my hash. The one sequence which has the key someIDnr. The other part gives me a looong output where there is every single sequence ran through the subroutine. Hmm actually even multiple times.

      @dr is not lexically scoped. The result is that each time the sub is called, unless you're reinitializing it somewhere that we're not seeing, @dr still has cruft in it from previous calls to the subroutine. It probably needs to be declared as my @dr.

      $done is also not lexically scoped, but that's not as important as you're doing an assignment to it rather than an append. push is like an append for an array. If the array already has junk in it, you're just adding to what's already there.


      Dave

        Oh man - enlightenment just hit me. Looked over my code and now I got how this poor array was filled over and over again with some quite random stuff in the different parts of my script. Really bad idea to use a subs like this with a globally defined array.

        Changing the subs code to..

        sub revcompl { my (@dna) = @_; my @dr=(); foreach my $segment (@dna) { my $revcomp = reverse($segment); $revcomp =~ tr/ACGTacgtMKRYBDHVmkrybdhv/TGCAtgcaKMYRVHDBkmyrvhdb/; push @dr, $revcomp; } $done = join('',@dr); return $done; }

        Worked out well. Thank you very much. The behaviour noticed before still weirds me out, guess it's time to study some more. Any more enlightening comments on what happened there are welcome by everybody :D