Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Why does this sub grab the whole hash

by naturalsciences (Beadle)
on Nov 11, 2011 at 17:50 UTC ( #937639=perlquestion: print w/replies, xml ) Need Help??
naturalsciences has asked for the wisdom of the Perl Monks concerning the following question:

I used this little piece of code grabbed in my bigger script as a subroutine.

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

I had a has in my script connecting bunch of identification numbers with sequences. When I wanted to grab a sequence by a ID and run it through the sub.Lets say ..

$stringforsequence=$hashofseqs{someIDnr}; $reversedsequence=revcompl($stringforsequence); print "$reversedsequence";

Then what I got was a printout of reverse complements for EVERY single sequence in %hashofseqs. (Printout that shows that subroutine is ran on all "value" strings contained in hash) For print command or other such manipulations $stringforsequence is exactly what I would expect from it, a single sequence that corresponds to a certain someIDnr key.

How come that this string somehow passes the whole of the hash into the subroutine? How could this be avoided? It is my first script actually containing some hashes. I try to learn a bit about them as they seem to be rahter useful elements for many a data manipulation. But as for now I'm rather dumbfounded.

Replies are listed 'Best First'.
Re: Why does this sub grab the whole hash
by davido (Archbishop) on Nov 11, 2011 at 17:56 UTC

    Alter your code as follows and see what comes out:

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


      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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://937639]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2017-06-29 09:13 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (657 votes). Check out past polls.