jaydot has asked for the wisdom of the Perl Monks concerning the following question:

hi to all monks!

i have problem, this has been going on for a week already. just want to know what should i do. i still a newbie in perl.. uhmm.. just want to ask if how should i make my foreach loop dynamic.. the array found on the foreach loop basically gets values from the txt file.and prints all possible combinations based from the options in the text file. the problem is that how do i change my code if another option would be placed on the txt file.. the code and the txt file are placed below.

TXT FILE (testcase) option1:apple,banana,orange option2:apple,banana,orange option3:pork,chicken,beef #location and name of txtfile my $testcase="/home/jay/testcase/" my @a= &getValues($testcase,"option1"); my @b= &getValues($testcase,"option2"); my @c= &getValues($testcase,"option3"); foreach $ao(@a) { foreach $bo(@b) { foreach $co(@c) { print "$ao$bo$co; } } } sub getValues() { # accept parameter local ($keyword) = $_[1]; local ($mytestcases) = $_[0]; # retrieve txtfile testcases rows open (TESTCASE, $mytestcases) || die "couldn't open the file!\n"; @raw_value = <TESTCASE>; close(TESTCASE); # locate keyword foreach $rowsxx (@raw_value) { # split row item to get keyword @row_values = split(":",$rowsxx); #print "@row_values[0]\n"; # checking of keyword [use eq to compare string] if ($keyword eq @row_values[0]) { # split @row_values[1] to get config values @config_values = split(",",@row_values[1]); return @config_values; last; } } }

if another option would be placed for example

option4;salt,sugar,vinegar
how do i change my code so that it would be dynamic in the sense that no matter how many options i place in my txt file it will all be evaluated.what i did in my foreach loop was basically a static one..basically i want the code to be dynamic so that any in the future if there are options to be added the user would just place values on the txt file without changing the code.. hope you can help me with this.. thanks in advance..

Replies are listed 'Best First'.
Re: how to make a foreach loop dynamic?
by moritz (Cardinal) on May 20, 2010 at 09:45 UTC

    I'm not quite sure if I understood your problem correctly, but Algorithm::Loops might solve it. At least it makes it easy to produce all possible combinations of dynamically computed lists.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: how to make a foreach loop dynamic?
by ahmad (Hermit) on May 20, 2010 at 11:32 UTC

    Looking at your code, here's what I have in mind as a solution

    It might not be perfect:

    #!/usr/bin/perl use strict; use warnings; # store everything in a hash where ( option = hash key ) my %HASH; while (<DATA>) { chomp; my @line = split /:/,$_; my $K = shift @line; my @V = split /,/ , shift @line; $HASH{$K} = \@V; } # options per line my $OPTIONS_PER_LINE = 3; foreach my $C (1..$OPTIONS_PER_LINE) { foreach my $i (sort keys %HASH) { print $HASH{$i}->[$C-1]; } print "\n"; } __DATA__ option1:apple,banana,orange option2:apple,banana,orange option3:pork,chicken,beef
Re: how to make a foreach loop dynamic?
by BioLion (Curate) on May 20, 2010 at 11:37 UTC

    Your reading in of the options file doesn't care how many options (or elements in each option) there are. I take it you want to replace your three-tier foreach loop? There are many modules for this sort of thing on CPAN, and also there is some example code on perlfaq4.

    The last example shows how to compute permutations for any array. HTH.

    Just a something something...

      thanks for the replies but im findin it hard to go on the sample code on perlfaq4.. i have trouble with understanding it. you see this is my first time creating a code like this.perl is my fist programming languange. and im having a hard time in doing all the coding..i have been starting the coding nearly two weeks already..and im a newbie to programming.. a little example will do just to guide me somehow..tnx in advance..

        Mayeb have a look at the source code for List::Permutor ( Source ). It is pretty well commented and should help.

        I would just use this module or similar to do the heavy lifting directly rather than trying to roll my own (Yes, even you can use CPAN) .

        Sorry I can't be of more help, time is short for me, but i hope you get this sorted out! I think the key lies in the way List::Permutor handles a list of *any* length, as opposed to your static three-tier loop. Have a look at that and see how you get on.

        Again, sorry I can't be more help.

        Just a something something...