in reply to Re^2: Variable number of foreach loops
in thread Variable number of foreach loops
BTW what is the third argument($i) nFor( $n-1, @_, $i ) doing here?
As the sub recurses, one of the array references is remove from the front of @_ at each level, and the current value being iterated by the for loop at that level is added to the end.
Once $n == 0, all the array references have been removed and $n has been shifted off, all that is left in @_, is the set of elements to be printed.
BTW. Here is a cleaner implementation that takes a callback to which the results sets are passed:
#! perl -slw use strict; sub nForX(&@) { my $code = shift; my $n = shift; return $code->( @_ ) unless $n; for my $i ( @{ shift() } ) { &nForX( $code, $n-1, @_, $i ); } } my @a = 1..10; my @b = 'a'..'z'; my @c = map chr, 33 .. 47; nForX { print join ' ', @_; } 3, \( @a, @b, @c );
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Variable number of foreach loops
by abhay180 (Sexton) on Nov 28, 2013 at 05:09 UTC | |
by hdb (Monsignor) on Nov 28, 2013 at 12:40 UTC | |
by BrowserUk (Patriarch) on Nov 28, 2013 at 05:20 UTC | |
by abhay180 (Sexton) on Nov 28, 2013 at 10:44 UTC | |
by BrowserUk (Patriarch) on Nov 28, 2013 at 11:50 UTC | |
by abhay180 (Sexton) on Nov 28, 2013 at 18:21 UTC | |
|
In Section
Seekers of Perl Wisdom