Click to See Complete Forum and Search --> : Looping through an array


Mrclean78
04-12-2003, 07:35 AM
I need to write a loop that parses form data against an array. The form data is in the scalar, $search. The array contains keywords that should be matched, and is called @Keywords. What type of loop should I do? I'm thinking a foreach because I need all values of the array checked. But I'm not sure how to get the program to search for each variable. Any thoughts? Thanks.


Scott

jeffmott
04-12-2003, 08:22 AM
You could use a for loop, but this is more of a situation for grep.my @matches = grep { /$search/ } @Keywords;

Mrclean78
04-12-2003, 10:15 AM
the new array, @matches will then take all of the keywords that match and store them. Now, I need to say something like this:

if (@matches "equals one or more words" in @Keywords)
{
Save to external file A; #I have this
}
else
{
Save to external file B;
}

What can I use for "equals one or more words"?

Thank you.


Scott

jeffmott
04-12-2003, 10:24 AM
If you mean by words that it matches one of more elements in @Keywords then it is as simple as:if (@matches > 1) {}