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

Re: [FWP] TMTOWTDI #2



Vicki Brown <vlb@cfcl.com> writes:

> # 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
[...]
>      # 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?

Here's an "any more", but probably not a "better":

    @pnames = qw(blastp blastn blastx tblastn tblastx);
    $re = join '|',@pnames;
    
    # Get $program by hook or by crook
    $program =~ /$re/o;

For robustness, you'd probably want to use something like
    $re = join '|', map {"\Q$_\E"} @pnames
instead.

Of course, for a large list of @pnames (but how many BLASTs are there, 
anyway?), you'd go with the hash method, but at least it's OMWTDI.

-- 
Ariel Scolnicov        |"GCAAGAATTGAACTGTAG"            |ariels@compugen.co.il
Compugen Ltd.          |Tel: +972-2-6795059 (Jerusalem)	\  NEW IMPROVED URL!
72 Pinhas Rosen St.    |Tel: +972-3-7658520 (Main office)`--------------------
Tel-Aviv 69512, ISRAEL |Fax: +972-3-7658555  http://3w.compugen.co.il/~ariels

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