Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Variable number of foreach loops

by BrowserUk (Patriarch)
on Nov 27, 2013 at 13:15 UTC ( #1064610=note: print w/replies, xml ) Need Help??


in reply to Variable number of foreach loops

Is this possible in PERL?

Yes:

#! perl -slw use strict; sub nFor { my $n = shift; if( $n ) { for my $i ( @{ shift() } ) { nFor( $n-1, @_, $i ); } } else { print join ' ', @_; } } my @a = 1..10; my @b = 'a'..'z'; my @c = map chr, 33 .. 47; nFor( 3, \@a, \@b, \@c ); __END__ C:\test>junk90 1 a ! 1 a " 1 a # 1 a $ 1 a % 1 a & 1 a ' 1 a ( 1 a ) 1 a * 1 a + 1 a , 1 a - 1 a . 1 a / 1 b ! 1 b " ... 10 y - 10 y . 10 y / 10 z ! 10 z " 10 z # 10 z $ 10 z % 10 z & 10 z ' 10 z ( 10 z ) 10 z * 10 z + 10 z , 10 z - 10 z . 10 z /

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^2: Variable number of foreach loops
by abhay180 (Sexton) on Nov 27, 2013 at 17:56 UTC
    Thanks a lot. BTW what is the third argument($i) nFor( $n-1, @_, $i ) doing here?
      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.
        thanks a lot. yes i get it now. In the same light.....i want to call nfor() with different number of arguments...
        nfor(1,\@a_1); nfor(2,\@a_1,\@a_2); nfor(3,\@a_1,\@a_2,\@a_3);...so on nfor(N,\@a_1,\@a_2,...\@a_N).
        I have a way...but what to check if there is a much crispier way to do it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2022-05-25 16:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (90 votes). Check out past polls.

    Notices?