Michael Alipio wrote:
Hi,

Hello,

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";
     }
  }
}


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.
Is there a better way of doing this?

You could use the Getopt::Long module to get multiple options into an array:

perldoc Getopt::Long
[ snip ]
Options with multiple values
    Options sometimes take several values. For example, a program could
    use multiple directories to search for library files:

        --library lib/stdlib --library lib/extlib

    To accomplish this behaviour, simply specify an array reference as
    the destination for the option:

        GetOptions ("library=s" => \...@libfiles);


Then to test them all:

if ( @options = grep $word =~ /$_/, @options ) {
    print "All pattern matched!\n";
    }



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to