Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Accessing hashes in main program from subroutine

by Angharad (Pilgrim)
on Jan 09, 2008 at 15:43 UTC ( [id://661382]=perlquestion: print w/replies, xml ) Need Help??

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

I have a script that contains a subroutine that takes a file, does some analysis, reads the results into two separate hashes and then returns the hashes to the main program. Or so I thought. When I do this in the subroutine just before sending the hashes back to the main program, it prints out the contents correctly
for my $key (keys %$tData) { my $value = $tData{$key}; print "overlap: $key $value\n"; }
I then send the hashes back to the program thus
return($tData, $aData);
The subroutine is called in the main program as follows:
my ($tOVERLAPData, $aOVERLAPData) = getOVERLAPData($fOVERLAP);
And then try to print out the contents of the same hashes within the same program but it seems that the hashes aren't being returned as I'm not getting any print out of the content. Can anyone suggest why this might be so? I know i haven't given you much code but I'm assuming I've just done something rather silly as returning a hash should be a trivial process should it not? The hashes DO exist in the subroutine and i can print out the contents just fine - I just can't get to them in the main program.

Replies are listed 'Best First'.
Re: Accessing hashes in main program from subroutine
by mickeyn (Priest) on Jan 09, 2008 at 15:58 UTC
    hi,

    seems like you have a bug in $tData{$key}
    this access %tData and NOT referenced hash $tData.

    try changing to $tData->{$key} and check again.

    BTW, use strict would have told you you're doing something wrong here ...

    HTH,
    Mickey

      thanks a lot. That worked a treat :)
Re: Accessing hashes in main program from subroutine
by toolic (Bishop) on Jan 09, 2008 at 15:59 UTC
    I think you mean to say that $tData and $aData are references to hashes. Without seeing more of your code, you might try debugging using ref and Data::Dumper to make sure you have what you think you have.
    use Data::Dumper; print Dumper(\$tdata);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://661382]
Approved by citromatik
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-19 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found