Welcome to the Monastery | |
PerlMonks |
Re^3: Dynamically create a foreach loop within a foreach loop?by smls (Friar) |
on Dec 11, 2013 at 12:22 UTC ( [id://1066613]=note: print w/replies, xml ) | Need Help?? |
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.) 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: It leaves the remaining array references in @_, and passes them on to a new function-call to the same function:
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. 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.)
In Section
Seekers of Perl Wisdom
|
|