Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^8: Variable number of foreach loops

by abhay180 (Acolyte)
on Nov 28, 2013 at 18:21 UTC ( #1064860=note: print w/ replies, xml ) Need Help??


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

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) } } }


Comment on Re^8: Variable number of foreach loops
Download Code
Re^9: Variable number of foreach loops
by BrowserUk (Pope) on Nov 28, 2013 at 19:15 UTC
    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.

        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:

        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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-09-20 11:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (158 votes), past polls