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) } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^9: Variable number of foreach loops
by BrowserUk (Patriarch) on Nov 28, 2013 at 19:15 UTC | |
by abhay180 (Sexton) on Nov 29, 2013 at 05:31 UTC | |
by BrowserUk (Patriarch) on Nov 29, 2013 at 08:54 UTC | |
by abhay180 (Sexton) on Nov 29, 2013 at 11:48 UTC |