On Thu, Nov 5, 2009 at 11:26 PM, Michael Alipio <daem0n...@yahoo.com> wrote:

> if I have a script that accepts any combination of the 5 or maybe even more
> options, say, option1, option2, option3...
>
>
> Now, after collecting the options, for each option, there is a
> corresponding regexp pattern. I will then build an if statement, where the
> test should be, all the options entered must match (&&) otherwise, return
> false.
> I'm thinking this can only be done by nested if's:
>
> if ($word =~ /$option1/ && $word =~ /$option2){
>  if ($word =~ /$option3/ && $word =~ /$option4){
>     if ($word =~ /$optionN/){
>        print "All pattern matched!\n";
>     }
>  }
> }
>

Can you reverse the logic?

if ($word !~ /$option1/) { print "Does not match option1\n" }
elsif ($word !~ /$option2/) { print "Does not match option2\n" }
elsif ($word !~ /$option3/) { print "Does not match option3\n" }
elsif ($word !~ /$option4/) { print "Does not match option4\n" }
elsif ($word !~ /$optionN/) { print "Does not match optionN\n" }
else { print "All pattern matched!\n" }


> Now I'm thinking, it is quite impossible to dynamically create all those if
> tests. Perhaps I can just open a file for writing, write a new perl script
> which will have those codes, and execute it at the end.
>

For more dynamic you can try something like this...

$match = 1;
map { $match = 0 if ($word !~ /$_/) } @options;
if ($match) { print "All pattern matched\n" }

Hope that helps.

-- 
Robert Wohlfarth

Reply via email to