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


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

I have stored my data in N arrays. N varies from 1..20 I have a random string, that i breakdown into some N-segments. Each Segment is then a array. Now i want to run nfor() on these. So as you can see segment size is random. Hence variable number of arrays.

Replies are listed 'Best First'.
Re^7: Variable number of foreach loops
by BrowserUk (Patriarch) on Nov 28, 2013 at 11:50 UTC

    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.

      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.