Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Foreach on an array (of hashes)

by LeGo (Chaplain)
on Nov 21, 2000 at 06:58 UTC ( [id://42642]=perlquestion: print w/replies, xml ) Need Help??

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

I am looking to run a foreach loop on my array where each element of that array is a hash. where my hash and array are established as below.
push @mass_info, {"PID" => $PID, "IO" => \@IO};
I am trying
foreach(@mass_info){ print "$_{PID} \n"; }
This does not work but this does.
$counter = 0; foreach (@mass_info){ print "$mass_info[$counter++]{PID}\n"; }
I would like to know how perl does this foreach loop the first way.

Thanks in advance.

LeGo

Replies are listed 'Best First'.
Re: Foreach on an array (of hashes)
by quidity (Pilgrim) on Nov 21, 2000 at 08:10 UTC

    The elements of @mass_info are references to annonymous hashes. If you were to just print out the value of $_, you would see something like this:

    HASH(0x804e054)

    The first snippet of code fails because you are trying to access the 'PID' element of the %_ hash, as this does not exist, then you get nothing output. If you had run perl with warnings on (-w) then you would have seen a warning about the use of an unitialised value where this was used.

    The second example works because you are dereferencing the reference held in $mass_info[0] by the use of the {PID}. Another way to write this is as $mass_info[0]->{PID} which is clearer, as it shows that you are accessing the PID element of the hash which is 'pointed' too by $mass_info[0].

    Your first loop can be made to work by using this syntax:

    foreach (@mass_info) { print $_->{PID}, "\n"; }

    If you had a hash with many elements stored in $mass_info[0] then you would need to iterate over the values held in it using a syntax like this:

    foreach my $hash_ref (@mass_info) { foreach (keys %{$hash_ref}) { print "$_ => ${$hash_ref}{$_}\n"; } }

    For more information, you should refer to the perldsc, perllol, perlreftut and perlref manpages. You have uncovered one of the most powerful features of perl, the way to easily create complicated data structures.

Re: Foreach on an array (of hashes)
by rlk (Pilgrim) on Nov 21, 2000 at 07:02 UTC
Re: Foreach on an array (of hashes)
by Fastolfe (Vicar) on Nov 21, 2000 at 19:23 UTC
    Others have adequately explained why $hash{$key} won't work (since this accesses %hash, not a reference in $hash). The reason why $array[$index]{$key} works when $array[$index] is a reference is because Perl makes it work for you. Perl tries to make accessing complex data structures easy for you, and since $array[$index] can't be anything but a reference to a hash, there's no harm or ambiguity in treating $array[$index}{$key} like $array[$index]->{$key}. You can reference any level of data this way.

    Please read perlref, perlreftut, perldsc and perllol for information about references and complex data structures with them.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-19 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found