http://qs321.pair.com?node_id=1066593

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

This one has me stumped. Maybe I'm going about it wrong. Let's say one has multiple hashes of data to iterate through, but the number of hashes can change. For right now, we'll say you have a total of 4 hashes to iterate through.

How would one have perl populate 4 foreach loops? Or is there a better way to iterate through the hashes that I'm not thinking about.

Basically, I want perl to build this for me:

foreach one(hash one) { foreach two(hash two) { foreach three(hash three) { foreach four(hash four) {} } } }
I love it when a program comes together - jdhannibal

Replies are listed 'Best First'.
Re: Dynamically create a foreach loop within a foreach loop?
by smls (Friar) on Dec 11, 2013 at 11:07 UTC
      Thanks for all the quick responses. I'll be sure to vote up a bunch of posts. I ventured over to the other thread and found this code. I don't really understand what is occurring, so I thought I could explain it to the best of my abilities, and you guys could help by filling in the gaps?

      #! perl -slw use strict; my @a = 1..3; my @b = 'a'..'f'; my @c = map chr, 33 .. 37; nFor( 3, \@a, \@b, \@c ); #3 sets the number of arrays to sift through +. sub nFor { my $n = shift; #I understand shift chops off the first element in +an array, but what purpose does that serve? Also, how are the arrays + (@a, @b, @c) referenced in this code? if( $n ) { #what exactly is $n? for my $i ( @{ shift() } ) { # I have no idea how $i get's it' +s value? nFor( $n-1, @_, $i ); #why does the top nFor call for 4 va +riables, while this one only calls for 3? } } else { print join ' ', @_; } }
      I love it when a program comes together - jdhannibal

        Short answer: You really should read through perlintro and perlreftut, and at least skim through perldata.

        Longer answer:

        my $n = shift; #I understand shift chops off the first element in an array, but what purpose does that serve?

        In Perl, parameters passed to a subroutine are stored in the @_ array. (See perlintro#Writing-subroutines.)
        When shift is called without an explicit argument, it operates on that array. (See shift.)

        The purpose of the quoted line is to remove the first element (i.e. the first subroutine parameter) from the @_ array and store it in $n.


        Also, how are the arrays (@a, @b, @c) referenced in this code?

        The function consumes the first array reference (i.e. \@a) via another shift:

        for my $i ( @{ shift() } ) {
        It leaves the remaining array references in @_, and passes them on to a new function-call to the same function:
        nFor( $n-1, @_, $i );

        for my $i ( @{ shift() } ) { # I have no idea how $i get's it's value?

        shift() removes the first element from @_ and returns it - in this case it happens to be an array reference.
        @{ ... } performs array dereferencing - i.e. when given an array reference, it returns the actual array. (See perlreftut and References quick reference).
        for my $i ( ... ) { ... } loops over all elements of the list defined within the round brackets, storing the current element in $i each time. (See perlintro#foreach.)


        nFor( $n-1, @_, $i ); #why does the top nFor call for 4 variables, while this one only calls for 3?

        When an array is placed in a comma-separated list, it is "flattened" - i.e. it's as though all its elements had been individually placed in that spot in the list, in order. (See perldata#List-value-constructors.)

Re: Dynamically create a foreach loop within a foreach loop?
by tobyink (Canon) on Dec 11, 2013 at 10:57 UTC
    use strict; use warnings; use List::MapMulti; my %sizes = (big => 'L', small => 'S'); my %colours = (red => '001', blue => '002', black => '000'); my %origins = (American => 'US', Japanese => 'JP', German => 'DE'); my %products = (car => '0001', truck => '0004'); my @hashes = (\%sizes, \%colours, \%origins, \%products); my @hash_keys = map [sort keys %$_], @hashes; mapm { my @keys = @_; my @values = map $hashes[$_]{$_[$_]}, 0 .. $#_; my $product_desc = join q[ ], @keys; my $product_code = join q[-], @values; printf "%s - %s\n", $product_code, $product_desc; } @hash_keys;
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      If I could vote this response more than once, I would..
Re: Dynamically create a foreach loop within a foreach loop?
by AnomalousMonk (Archbishop) on Dec 11, 2013 at 10:58 UTC

    Take a look at Data::Dumper or Data::Dump (and many others of similar nature). Both of these modules define functions that iterate through arbitrarily deeply nested data structures — and they don't even require that they be purely hashes-of-hashes or arrays-of-arrays!

    >perl -wMstrict -le "my %ds = ( X => [ { one => 1 }, { two => 2, three => 3 }, ], Y => [ { fee => 'fie' }, { foe => 'fum' }, { foo => 'bar' }, ], ); ;; use Data::Dumper; print Dumper \%ds; " $VAR1 = { 'X' => [ { 'one' => 1 }, { 'three' => 3, 'two' => 2 } ], 'Y' => [ { 'fee' => 'fie' }, { 'foe' => 'fum' }, { 'foo' => 'bar' } ] };
      wrong parent, reap
Re: Dynamically create a foreach loop within a foreach loop?
by choroba (Cardinal) on Dec 11, 2013 at 11:01 UTC
    See Recursion.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Dynamically create a foreach loop within a foreach loop?
by ikegami (Patriarch) on Dec 11, 2013 at 15:03 UTC
    use Algorithm::Loops qw( NestedLoops ); my @hashes = ( \%hash0, \%hash1, \%hash2, \%hash3 ); my @key_lists = map [ keys %$_ ], @hashes; NestedLoops(\@key_lists, sub { my @keys = @_; say join ', ' map "$keys[$_] => $hashes[$_]{$keys[$_}}", 0..$#keys; });
Re: Dynamically create a foreach loop within a foreach loop?
by karlgoethebier (Abbot) on Dec 11, 2013 at 10:56 UTC

    Hi jdlev!

    A good point to start is HASHES OF HASHES.

    BTW, i assume that you are aware that your code isn't valid Perl.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Dynamically create a foreach loop within a foreach loop?
by educated_foo (Vicar) on Dec 11, 2013 at 15:28 UTC
    As mentioned elsewhere, you can use recursion:
    sub foo { my ($hash, @path) = @_; while (my ($k, $v) = each %$hash) { if (ref $v eq 'HASH') { foo($v, @path, $k); } else { # whatever } } }
    You can also use "eval" to build up the loops (hand-waving a bit):
    $depth = compute_hash_depth; $code = '...'; for (reverse 1..$depth) { my $prev = $_-1; $code = "while (my (\$k$_, \$v$_) = each \$v$prev) { $code }"; } eval $code;
    In general, when you want nested loops and don't know how many beforehand, recursion is easiest.
Re: Dynamically create a foreach loop within a foreach loop?
by kcott (Archbishop) on Dec 11, 2013 at 15:56 UTC

    G'day jdlev,

    Here's an example using recursion. You'll need to modify this depending on what you want to do in the innermost loop: I just print the values. Also note that I've used 'sort keys %hash' to get consistent output; you may not need the additional overhead of sorting (in this case, just use 'keys %hash').

    #!/usr/bin/env perl use strict; use warnings; my %x = ( a => 1, b => 2 ); my %y = ( c => 3, d => 4 ); my %z = ( e => 5, f => 6 ); multi_for([\%x, \%y, \%z]); sub multi_for { my ($hashes, $values, $index) = @_; $values = [] unless defined $values; $index = 0 unless defined $index; if ($#$hashes >= $index) { my %hash = %{$hashes->[$index]}; for (sort keys %hash) { multi_for($hashes, [@$values, $hash{$_}], $index + 1); } } else { # Do something with $values, e.g. print "@$values\n"; } }

    Output:

    1 3 5 1 3 6 1 4 5 1 4 6 2 3 5 2 3 6 2 4 5 2 4 6

    -- Ken

Re: Dynamically create a foreach loop within a foreach loop?
by AnomalousMonk (Archbishop) on Dec 13, 2013 at 07:42 UTC