bcole23 has asked for the wisdom of the Perl Monks concerning the following question:

my $DB; my @DB = qw(DB1 DB2); &select_db(); if (!$DB) {&select_db();} if ($DB eq 'Q') {die;} sub select_db { print "Select a database: "; $DB = <STDIN>; for (@DB) { if ($DB =~ /$_/i) { return $DB; } } }
All I want is to make sure the entry is in the array and allow a nice way to try again or exit. I know there are really nice easy ways in probably one line of code to do this...... Your help is appreciated.

Replies are listed 'Best First'.
Re: really easy ? but want nice succinct code
by derby (Abbot) on Aug 16, 2002 at 20:50 UTC
    #!/usr/local/bin/perl -wd my $DBS = {DB1=>1,DB2=>1,Q=>1}; my $db = select_db($DBS); sub select_db { my( $dbs ) = shift; my( $adb ); while(1) { print "Select a database: "; $adb = <STDIN>; chomp($adb); last if $dbs->{uc $adb}; } $adb; }
Re: really easy ? but want nice succinct code
by DamnDirtyApe (Curate) on Aug 16, 2002 at 20:58 UTC

    Since I assume you don't have two databases with the same name, making them hash keys makes the test much easier.

    #! /usr/bin/perl use strict ; use warnings ; $|++ ; my $DB = 0 ; my %DB_list = map { $_ => 1 } qw( DB1 DB2 ) ; until ( $DB_list{ $DB } ) { print "Select a database: " ; chomp( $DB = <STDIN> ) ; $DB = uc $DB ; if ( $DB eq 'Q' ) { print "Quitting...\n" and exit } } print "You chose $DB.\n" ; __END__

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
(jeffa) Re: really easy ? but want nice succinct code
by jeffa (Bishop) on Aug 16, 2002 at 21:06 UTC
    use strict; my $DB; my @DB = qw(DB1 DB2); { $DB = select_db(); redo unless grep {$_ eq $DB} (@DB,'Q'); } die if $DB eq 'Q'; sub select_db { print "Select a database: "; chomp($_ = <STDIN>); return $_; }
    UPDATE:
    Specified STDIN inside the angle brackets - thanks for the head's up Zaxo. I was being too succinct and forgot that even though my original code works, it tries to open any arguments one might accidentaly supply on the command line. Oops. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: really easy ? but want nice succinct code
by tommyw (Hermit) on Aug 16, 2002 at 21:00 UTC

    my @valid=qw(DB1 DB2); do { print "Enter a DB choice: "; $choice=<STDIN>; chomp $choice; die if $choice eq 'Q'; } until grep {$_ eq $choice} @valid;

    --
    Tommy
    Too stupid to live.
    Too stubborn to die.

Re: really easy ? but want nice succinct code
by BrowserUk (Patriarch) on Aug 16, 2002 at 23:38 UTC

    #! perl -w my @dbs = <DATA>; my $db= "\n"; print "Select DB from:\n @dbs or Q to quit:" and $db = uc(<>) while( $db eq "\n" || "@dbs\nQ\n" !~ m/$db/s); chomp $db; $db eq 'Q' and die "Quiting...\n"; print "\n$db selected\n"; __DATA__ DB1 DB2

    Looks like this is use:

    C:\test>190748 Select DB from: DB1 DB2 or Q to quit: Select DB from: DB1 DB2 or Q to quit:fred Select DB from: DB1 DB2 or Q to quit:db2 DB2 selected C:\test>190748 Select DB from: DB1 DB2 or Q to quit:q Quiting... C:\test>

    What's this about a "crooked mitre"? I'm good at woodwork!
      That is a thing of beauty. Thank you all for your replies. I've learned quite a bit from this. Brandon