Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^5: Variable number of foreach loops

by BrowserUk (Patriarch)
on Nov 28, 2013 at 05:20 UTC ( #1064740=note: print w/replies, xml ) Need Help??


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

Where will you get the variable number of arrays from? And how will you be storing them?


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^6: Variable number of foreach loops
by abhay180 (Sexton) on Nov 28, 2013 at 10:44 UTC
    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.

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

Log In?
Username:
Password:

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

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



    Results (81 votes). Check out past polls.

    Notices?