http://qs321.pair.com?node_id=1064808


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

That's not very clear. Could you post a little code to show me what you mean?

For example, if you have code like this:

my( @a, @b, @c ); ## stuff here to fill the arrays

What happens if you need another array @d?


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^8: Variable number of foreach loops
by abhay180 (Sexton) on Nov 28, 2013 at 18:21 UTC

    Here i have pasted sample code in which i have various combinations being generated using multiple arrays. In the code , gen_loops() is ur nfor(). I want to be able to call gen_loops() for multiple number of array elements.

    @X = ("X1","X2","X3"); @Y = ("Y1","Y2","Y3"); @Z = ("Z1","Z2","Z3"); $HDR_SCHEMA_XY = "X::Y"; print_combinations($HDR_SCHEMA_XY); $HDR_SCHEMA_XYZ = "X::Y::Z"; print_combinations($HDR_SCHEMA_XYZ); sub print_combinations { my $sub_hdrs = shift; my @sub_hdrs = split(/::/,$sub_hdrs); my $subh_count=0; foreach my $i (@sub_hdrs) { @{"array_".$subh_count} = @{ $i }; $subh_count++; } gen_loops(2,\@array_0,\@array_1); } sub gen_loops { my $loop_count = shift; if ($loop_count==0) { print join "+",@_,"\n"; } else { for my $i ( @{ shift() } ) { gen_loops($loop_count-1,$iter_count,@_,$i) } } }
      I want to be able to call gen_loops() for multiple number of array elements.

      I still do not understand what problem you are having?

      If you want to call it with 3 arrays: call it with 3.

      If you want to call it with 4 arrays; call it with 4.

      If you ....

      Instead of repeating the same thing over and over; try showing us what you are trying to do that isn't working.

      Ie. DESCRIBE THE PROBLEM YOU ARE HAVING?


      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.
        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.