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

Hello,
I'm a total newbie regarding Perl and I am currently stuck at a (probably super easy solvable) problem:
#!/user/bin/perl -w use warnings; use strict; 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);

I have tried iterating with foreach and the hash itself
foreach (keys $hash){ print $_ . "\n"; }

So far so good but when I tried it with the array:
foreach (keys $bla[0]){ print $_ . "\n"; }

it didn't work. I've also tried to use {$bla[0]}, but it gives me this strange message:
Odd number of elements in anonymous hash

Thanks in advance

Replies are listed 'Best First'.
Re: Using foreach to iterate through a hash in an array
by choroba (Cardinal) on Aug 30, 2017 at 11:28 UTC
    my @bla = (%hash, %hash2); doesn't create an array of hashes, it flattens the hashes to key - value pairs, so @bla in fact contains
    "00:20:00", "a", "10:00:00", "b", "00:00:00", "a",
    etc. (in no particular order).

    To create an array of hashes, use references:

    my @bla = (\%hash, \%hash2);

    You can then iterate with dereference

    for (keys %{ $bla[0] }) {

    or, in recent Perls,

    for (keys $bla[0]->%*)

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Oh ok thank you. It works and it seems as if I have skipped a few tutorial pages. I will have to take another look into it and thanks again.
        I have tried iterating with foreach and the hash itself

        foreach (keys $hash){
            print $_ . "\n";
        }

        So far so good ...

        Given the code you OPed, it's hard to imagine this would have done anything "good" unless  $hash.was actually a hash reference. If this was the case, please be aware of this from the latest Perl documentation:

        Starting with Perl 5.14, an experimental feature allowed keys to take a scalar expression. This experiment has been deemed unsuccessful, and was removed as of Perl 5.24.
        This experimental ex-feature was also associated with values, each, push, pop and friends.


        Give a man a fish:  <%-{-{-{-<

Re: Using foreach to iterate through a hash in an array
by AnomalousMonk (Archbishop) on Aug 30, 2017 at 15:27 UTC
Re: Using foreach to iterate through a hash in an array
by thanos1983 (Parson) on Aug 30, 2017 at 15:41 UTC

    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!