I had a thought after my last post - what if, in the future, you want to do an "and" type search on multiple key words? My first attempt was to build up a string of greps, then eval the string:<br><FONT FACE=monospace><br>@Array = ("Andy¦moretext¦windows¦nothanks", "andy¦sometext¦perl¦unix", "Fred¦othertext¦windows¦yesplease"

;<br><br>@To_Find = ("Andy", "Unix"

;<br><br># Set up a string of greps to do an "and" type search on multiple keywords.<br>foreach $Word (@To_Find) {<br> $Search_String = $Search_String . "grep(/" . <br> $Word . "/i, ";<br>}<br><br># Terminate the search string.<br>$Search_String .= "\@Array" . "

" x ($#To_Find + 1) . ";";<br><br>print $Search_String, "\n";<br><br># Evaluate the search string, putting the results into @Found.<br>@Found = eval($Search_String);<br><br>print @Found;<br></font><br>Then I realised that a slighter easier to understand method might be to just loop around each keyword and grep for it:<br><FONT FACE=monospace><br>@Array = ("Andy¦moretext¦windows¦nothanks", "andy¦sometext¦perl¦unix", "Fred¦othertext¦windows¦yesplease"

;<br><br>@To_Find = ("Andy", "Unix"

;<br><br># Take a copy of @Array that will be reduced until we have <br># through each keyword.<br>@Search_This = @Array;<br><br># Now loop around each keyword.<br>foreach $Keyword (@To_Find) {<br> # Each time around the loop, @Search_This will only<br> # contain items that contain the current key word.<br> @Search_This = grep(/$Keyword/i, @Search_This);<br>}<br><br>print @Search_This;<br></font><br>And then I got to wondering how I would do an "OR" type search and came up with:<br><FONT FACE=monospace><br>@Array = ("Andy¦moretext¦windows¦nothanks", "john¦sometext¦perl¦unix", "Fred¦othertext¦windows¦yesplease"

;<br><br>@To_Find = ("Andy", "Unix"

;<br><br># Join the keywords together into a single pipe separated<br># string. Grep will treat this as a "or" type search.<br># Note that the "¦" is not related to the "¦" characters <br># in the original string. It is a regex "or" operator.<br>$Keyword_List = join("¦", @To_Find);<br><br>@Found = grep(/$Keyword_List/i, @Array);<br><br>print @Found;<br></font><br><br>Anyway, it livened up a boring lunch time, and I hope it helps someone

<p> <br><a href=mailto: > </a><br><a href= > </a><br>--<br>
0 1 - Just my two bits