kleinbiker7 has asked for the wisdom of the Perl Monks concerning the following question:
I dont know how to do it.
For example, let's say I want to search for both the words cat and dog in a string. The code would look like this:
I heard about the eval function, but I dont know how to use it! Any help will be most welcome! I feel that Perl has a way of evaluating expressions that are created on the fly.$str = "cat dog rat elephant giraffe"; $searchstr = "cat dog"; @array = split (/\s+/, $searchstr); foreach $animal (@array) { # Here I build my IF statement, which I will execute # when I have finished my loop! $execute .= "$str =~ /$animal/ && "; } #finish off the string...chop off hanging && $execute =~ s/ && //; # HOW DO I DO THIS??? # THIS OBVIOUSLY DOESNT WORK !!! if ($execute} { print "Found a DOG and a CAT!\n\n"; }
Cheers!
Robert
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 1: How to generate Perl statements on the fly and execute them when they are synthesized.
by tilly (Archbishop) on Oct 17, 2001 at 20:45 UTC | |
|
Re: How to generate Perl statements on the fly and execute them when they are synthesized.
by Masem (Monsignor) on Oct 17, 2001 at 20:52 UTC | |
|
Re: How to generate Perl statements on the fly and execute them when they are synthesized.
by Caillte (Friar) on Oct 17, 2001 at 20:25 UTC | |
|
Re: How to generate Perl statements on the fly and execute them when they are synthesized.
by earthboundmisfit (Chaplain) on Oct 17, 2001 at 20:40 UTC | |
|
Re: How to generate Perl statements on the fly and execute them when they are synthesized.
by DrManhattan (Chaplain) on Oct 18, 2001 at 00:47 UTC | |
|
Re: How to generate Perl statements on the fly and execute them when they are synthesized.
by tommyw (Hermit) on Oct 17, 2001 at 20:48 UTC |