[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] TMTOWTDI #2



# Problem - we want code to validate input.
# In this particular case, the possible input values are
#     blastx, blastn, blastp, tblastx, tblastn
# so it looks like a job for a regexp
 
     # The program is interactive, as:
        print "Program name: ";
        chomp ($program = <STDIN>);


     # first attempt
 
     die "invalid program name\n" unless ($program =~ /t*blast[nxp]/);

 





     # Yes, but but this allows tttttttblastn as well as the (illegal) tblastp
 


     # second attempt
 
     die "invalid program name\n"
          unless ((/t?blast[npx]/) && ($_ ne "tblastp"));

     # which works...
 
 

     # TMTOWTDI
 
     my @pnames = qw(blastp blastn blastx tblastn tblastx);
     my %programs;
     @programs{@pnames} = ("") x @pnames;
 
     # ...
     die unless (exists $programs{$program});
 
     # which can also be used to provide hints to the user, as:

        unless (exists $programs{$program}) {
            warn "invalid program name; Choose from:\n" ;
            foreach $key (keys(%programs)) {
                print "$key "
            }
            print "\n";
            undef $program;
        }

# Any more?  Better?
-- --
        |\      _,,,---,,_       Vicki Brown <vlb@cfcl.com>
  ZZZzz /,`.-'`'    -.  ;-;;,_   Journeyman Sourceror: Scripts & Philtres
       |,4-  ) )-,_. ,\ (  `'-'  P.O. Box 1269  San Bruno  CA  94066
      '---''(_/--'  `-'\_) http://www.cfcl.com/~vlb  http://www.macperl.com

==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org