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

How to pass multidimensional hashed arrays as reference to a subroutine?

by avenka1 (Initiate)
on Sep 01, 2008 at 09:21 UTC ( [id://708151]=perlquestion: print w/replies, xml ) Need Help??

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

Hi The following is a way of passing a hash to a sub routine by reference. mysub(\%hash); sub mysub { my $params = shift; my %paramhash = %$params; } Now in the previous case let us say instead of a "%hash" I actually have a "anonymous array of hashes" - that is "@{$hash{$key}". How do I pass such an hash to a subroutine by reference? Would be great if you can reply to this. P.S: I am able to create such an hash and print it - but I am not able to pass it to a subroutine and have this print from the subroutine. Thanks Anand
  • Comment on How to pass multidimensional hashed arrays as reference to a subroutine?

Replies are listed 'Best First'.
Re: How to pass multidimensional hashed arrays as reference to a subroutine?
by themage (Friar) on Sep 01, 2008 at 09:31 UTC
    Hi avenka1,

    If you want to pass that array of hash as a reference, you can do it almost like you did with your hash, like this:
    mysub(\@arrofhash); sub mysub { my @aoh=@{$_[0]}; $aof[0]->{somekey}++; ... }


    But if that array is the only parameter to your sub, you can pass you array directly, like this:
    mysub(@arrofhash); sub mysub { my @aoh=@_; $aof[0]->{somekey}++; ... }
    And, obviously, you can do the same with your hash, if you mysub will be passed is that hash, like this:
    mysub(%hash); sub mysub { my %args=@_; print $args{somekey},"\n"; ... }
Re: How to pass multidimensional hashed arrays as reference to a subroutine?
by jethro (Monsignor) on Sep 01, 2008 at 10:02 UTC

    @{$hash{$key}} is not accessing an array of hashes, it accesses a hash of arrays. "hash" is here the name of the outer hash, the arrays are anonymous

    Remember that a hash of arrays (or hashes) is, if you want to pass the complete hash, not different from any simple hash with integers or strings in it. The inner hashes (or arrays) are stored in the outer hash just as a reference.

    So the outer hash can be handled exactly like you would any other normal hash, only when you access the values you should access them as a reference.

    if you want to pass a reference to an inner array or hash to a subroutine, just use mysub($hash{$key}) as there is already a reference stored in the hash.

Re: How to pass multidimensional hashed arrays as reference to a subroutine?
by broomduster (Priest) on Sep 01, 2008 at 09:50 UTC
    When you define your anonymous array of hashes, it automagically creates the reference you need, so you just need to save it (the reference, that is) somewhere. Then you can pass it to a subroutine in the usual way.

    use strict; use warnings; use Data::Dumper; my $array_of_hashes_ref = [ { h1key1 => "h1val1", h1key2 => "h1val2", }, { h2key1 => "h2val1", h2key2 => "h2val2", h2key3 => "h2val3", }, ]; print Dumper( $array_of_hashes_ref ); test_sub( $array_of_hashes_ref ); sub test_sub { my $aoh_ref = shift; print Dumper( $aoh_ref); }

    Output:
    $VAR1 = [ { 'h1key2' => 'h1val2', 'h1key1' => 'h1val1' }, { 'h2key2' => 'h2val2', 'h2key3' => 'h2val3', 'h2key1' => 'h2val1' } ]; $VAR1 = [ { 'h1key2' => 'h1val2', 'h1key1' => 'h1val1' }, { 'h2key2' => 'h2val2', 'h2key3' => 'h2val3', 'h2key1' => 'h2val1' } ];

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-24 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found