Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: 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.)
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.)

In reply to Re^3: Dynamically create a foreach loop within a foreach loop? by smls
in thread Dynamically create a foreach loop within a foreach loop? by jdlev

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2022-07-07 12:59 GMT
Find Nodes?
    Voting Booth?

    No recent polls found