Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

passing multiple hashes to a subroutine

by Jaapx (Initiate)
on Oct 19, 2011 at 12:03 UTC ( #932374=perlquestion: print w/replies, xml ) Need Help??

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

Hi Perl Monks,

I want to pass two hashes and a scalar to a subroutine and do a little work with them. Problem I'm getting is that the passed hashes don't seem to be recognized, as in, the if statement gives a "use of uninitialized value". Do you guys have any idea how to pass the hashes so that you can edit them? Many Thanks.

sub count_orientation($ \% \%){ my $ref = shift; my %x = %{(shift)}; my %y = %{(shift)}; $x{$ref}{amount}++; if ($y{$ref}{orientation} eq "for"){ $x{$ref}{amountFor}++; } else { $x{$ref}{amountRev}++; } } &count_orientation ($ref_end, %error_start, %score);

Replies are listed 'Best First'.
Re: passing multiple hashes to a subroutine
by Sewi (Friar) on Oct 19, 2011 at 12:32 UTC

    Hashes and arrays are the same in array context. Try print join(", ", @_);at the beginning of your sub to see the effect.

    I suggest passing references instead of hashes:

    sub count_orientation { my $ref = shift; my $x = shift; my $y = shift; $x->{$ref}{amount}++; if ($y->{$ref}{orientation} eq "for"){ $x->{$ref}{amountFor}++; } else { $x->{$ref}{amountRev}++; } } &count_orientation ($ref_end, \%error_start, \%score);

    Always remember that you're working on your original hashes when using references:

    sub foo { my $hashref = shift; delete $hashref->{a}; print keys %$hashref; } %x = (a => 1, b => 2); foo(\%x); print $x{a};
    The sub will modify the original %x and $x{a} is gone away at the last line.

Re: passing multiple hashes to a subroutine
by Anonymous Monk on Oct 19, 2011 at 12:17 UTC

    using amperstand &func() ignores prototypes, so use func ...; instead

    Or forget about prototypes and use  func3scalars( $scalar, \%two, \%three )

    See Modern Perl: the free book

Re: passing multiple hashes to a subroutine
by AnomalousMonk (Bishop) on Oct 19, 2011 at 19:46 UTC
    sub count_orientation($ \% \%){

    I was a bit surprised to find that spaces in a prototype declaration are accepted. A quick perusal of the perlsub documentation for ActiveState 5.8.9 and Strawberries 5.10.1 and 5.12.3 reveals no mention of whitespace. Ya learn somethin' every day.

    The general consensus regarding prototypes seems to be that they were a good idea that was only 60%-80% baked, and that one should probably not use them unless one knows exactly how they work; the fact that Jaapx is calling a prototyped function in such a way as to defeat prototyping suggests he or she does not.

Re: passing multiple hashes to a subroutine
by anneli (Pilgrim) on Oct 20, 2011 at 21:13 UTC

    Avoid prototypes unless you really need to use them; they're not like argument/parameter lists in other languages (frankly I think your use here will confuse anyone reading your code!).

    Instead, just pass references to the hashes. Thus:

    sub count_orientation { my $ref = shift; my %x = %{(shift)}; # this is still correct! my $y = %{(shift)}; # ... } count_orientation($x, \%error_start, \%score); # or: count_orientation($x, {%error_start}, {%score});
Re: passing multiple hashes to a subroutine
by itsscott (Sexton) on Oct 20, 2011 at 19:26 UTC
    I asked the same thing once too here's the answer I used to great success. And a small snippet from my working code.
    sub report { our %hash1; local *hash1 = $_[0]; our %hash2; local *hash2 = $_[1]; our %hash3; local *hash3 = $_[2]; my $othervar1 = $_[3]; my $othervar2 = $_[4]; ... do stuff ... } # call it report(\%hash1, \%hash2, \%hash3, $othervar1, $othervar2);
    Hope that helps

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://932374]
Approved by moritz
Front-paged by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2022-07-02 08:34 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (102 votes). Check out past polls.