jaydot has asked for the wisdom of the Perl Monks concerning the following question:
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
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..option4;salt,sugar,vinegar
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how to make a foreach loop dynamic?
by moritz (Cardinal) on May 20, 2010 at 09:45 UTC | |
|
Re: how to make a foreach loop dynamic?
by ahmad (Hermit) on May 20, 2010 at 11:32 UTC | |
|
Re: how to make a foreach loop dynamic?
by BioLion (Curate) on May 20, 2010 at 11:37 UTC | |
by jaydot (Novice) on May 21, 2010 at 07:41 UTC | |
by BioLion (Curate) on May 21, 2010 at 15:42 UTC |