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

Re: Using foreach to iterate through a hash in an array

by thanos1983 (Parson)
on Aug 30, 2017 at 15:41 UTC ( [id://1198341]=note: print w/replies, xml ) Need Help??


in reply to Using foreach to iterate through a hash in an array

Hello Anonymous Monk,

It seems that the fellow Monks have already answered your question. Every now and then I try to add minor step to add something extra.

If you want to get the key and value for each hash element you can use each. Also it will help you a lot to debug your complex elements with Data::Dumper.

Sample of code:

#!/user/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; my %hash = ( "00:00:00" => "a", "00:20:00" => "a", "10:00:00" => "b", "20:00:00" => "a", "02:22:00" => "b", ); my %hash2 = ( "00:00:00" => "a", "00:20:00" => "a", "10:00:00" => "b", "20:00:00" => "a", "02:22:00" => "b", ); my @bla = (\%hash, \%hash2); # print Dumper \@bla; foreach my $array_hash_element ( @bla ) { # reset the internal iterator so a prior each() doesn't affect the + loop keys %$array_hash_element; say $array_hash_element; # to see when the next hash will come in while( my ( $key , $value ) = each %$array_hash_element) { say "Key: $key, Value: $value"; } } __END__ $ perl test.pl HASH(0x55a097458d28) Key: 20:00:00, Value: a Key: 00:00:00, Value: a Key: 00:20:00, Value: a Key: 02:22:00, Value: b Key: 10:00:00, Value: b HASH(0x55a097470488) Key: 02:22:00, Value: b Key: 10:00:00, Value: b Key: 20:00:00, Value: a Key: 00:00:00, Value: a Key: 00:20:00, Value: a

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

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

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

    No recent polls found