Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^10: Variable number of foreach loops

by abhay180 (Acolyte)
on Nov 29, 2013 at 05:31 UTC ( #1064897=note: print w/replies, xml ) Need Help??


in reply to Re^9: Variable number of foreach loops
in thread Variable number of foreach loops

Dude, Not sure what i have repeated again and again..you asked for code...i showed you. Not sure how else you want to represent. The arguments i am passing to print_comb() function can create "variable" number of arrays. So i should be able to pass this variable-arrays to gen_loops(). I cannot have a static call like:
gen_loops(2,\@array_1,\@array_2)
and i want it like...
gen_loops($count, < "$count" number of arrays>)
Not sure if i am able to explain. Anyways i have figured it out in some round-about manner. Thanks for your help.

Replies are listed 'Best First'.
Re^11: Variable number of foreach loops
by BrowserUk (Pope) on Nov 29, 2013 at 08:54 UTC

    Had the question been phrased as:

    I have a fixed number of arrays to permute, but I want to be able to pick a (sub)selection of those arrays at runtime using a string argument.

    Then I might have come up with something like:

    #! 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 %stuff = ( X => [ qw[ X1 X2 X3 ] ], Y => [ qw[ Y1 Y2 Y3 ] ], Z => [ qw[ Z1 Z2 Z3 ] ], ); print "@ARGV"; my $pat = $ARGV[0] // 'X::Y'; my @keys = split '::', $pat; nForX { print "@_"; } scalar @keys, @stuff{ @keys }; __END__ C:\test>junk X1 Y1 X1 Y2 X1 Y3 X2 Y1 X2 Y2 X2 Y3 X3 Y1 X3 Y2 X3 Y3 C:\test>junk X::Z X::Z X1 Z1 X1 Z2 X1 Z3 X2 Z1 X2 Z2 X2 Z3 X3 Z1 X3 Z2 X3 Z3 C:\test>junk X::Y::Z X::Y::Z X1 Y1 Z1 X1 Y1 Z2 X1 Y1 Z3 X1 Y2 Z1 X1 Y2 Z2 X1 Y2 Z3 X1 Y3 Z1 X1 Y3 Z2 X1 Y3 Z3 X2 Y1 Z1 X2 Y1 Z2 X2 Y1 Z3 X2 Y2 Z1 X2 Y2 Z2 X2 Y2 Z3 X2 Y3 Z1 X2 Y3 Z2 X2 Y3 Z3 X3 Y1 Z1 X3 Y1 Z2 X3 Y1 Z3 X3 Y2 Z1 X3 Y2 Z2 X3 Y2 Z3 X3 Y3 Z1 X3 Y3 Z2 X3 Y3 Z3

    But then, if you'd taken the time to understand what you were trying to do, enough that you could express it clearly, you could probably have arrived at the solution yourself.

    FWIW: The problem you are having has little or nothing to do with how to call a subroutine, and everything to do with how to use or avoid symbolic references. (Read all three parts!)


    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 for Pe(a)rl(s) of wisdom! I cannot agree more. Yes, i think i went too voyeuristic by using symbolic references instead of the usual hashes and array-of-arrays.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1064897]
help
Chatterbox?
[LanX]: Eily I'm duing this constantly with people from rival cities ...MUCHO fun!
[Eily]: The best example of that is the TGV, they couldn't decide who would get it so it's in the middle of nowhere halfway between the two
[LanX]: Eily: that rings a bell
[erix]: interesting difference, I suppose they use different data/routes (and shorter seems better, no?)
[LanX]: Montabaur station
[erix]: (I used http://afstandmete n.nl/ )
LanX The stations of Limburg Süd and Montabaur, which are approximately 20 km apart, ...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2017-12-13 15:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (369 votes). Check out past polls.

    Notices?