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

Re^2: Dynamically create a foreach loop within a foreach loop?

by jdlev (Scribe)
on Dec 11, 2013 at 11:52 UTC ( #1066609=note: print w/ replies, xml ) Need Help??


in reply to Re: Dynamically create a foreach loop within a foreach loop?
in thread Dynamically create a foreach loop within a foreach loop?

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


Comment on Re^2: Dynamically create a foreach loop within a foreach loop?
Download Code
Re^3: Dynamically create a foreach loop within a foreach loop?
by smls (Friar) on Dec 11, 2013 at 12:22 UTC

    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.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2014-09-01 19:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (17 votes), past polls