Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Yes No Validation

by J9 (Beadle)
on Feb 06, 2003 at 18:11 UTC ( [id://233215]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I have a REALLY newbie question. I want to ask a question from the cmd line and get the answer in. But I want to validate the answer. I have this so far but it seems like far to many lines for what should be a simple thing.
#! perl.exe print "Please enter you name:\n"; chomp ($name =<STDIN>); &answer(); exit; sub answer() { print "Hello $name.\nWill you be joining me for a swim? [Y/N] \n"; chomp ($_=<STDIN>); if (/^[yn]/i) { if (/^n/i) { print "Begone, evil peon!\n" } if (/^y/i) { print "YIPPEEE.!!!!\n"; } } else { # Validate user input and return message if invalid print "\n\n=====================================\n"; print "You Have Entered and Incorrect Option\n"; print "Valid Options are [Y/N]\n"; print "=====================================\n\n"; &answer; } }

My above code also validates Yesterday as true and never as false. I only want to match [Yy]es, [Nn]o or just [Yy] or [Nn].

Replies are listed 'Best First'.
Re: Yes No Validation
by chromatic (Archbishop) on Feb 06, 2003 at 18:16 UTC

    Ask for them in your regex.

    if (/^((?:[Yy](?:es))|([Nn](?:o))$/) { .... }
Re: Yes No Validation
by lestrrat (Deacon) on Feb 06, 2003 at 18:18 UTC

    something like this?

    if( /^[Yy](?:es)?$/ ) { # do what you do for "yes" } elsif( /^[Nn]o?$/ ) { # do what you do for "no" } else { # error }
Re: Yes No Validation
by steves (Curate) on Feb 06, 2003 at 18:19 UTC

    Your regexps are matching anything that starts with 'y' or 'n' (any case). Just expand and anchor the matches to look like this:

    if (/^y|yes|n|no$/i) { if (/^no?$/i) { print "Begone, evil peon!\n" } if (/^y(es)?$/i) { print "YIPPEEE.!!!!\n"; } } else { # Validate user input and return message if invalid print "\n\n=====================================\n"; print "You Have Entered and Incorrect Option\n"; print "Valid Options are [Y/N]\n"; print "=====================================\n\n"; &answer; }

      That first Regexp will fart. See, the way it's written, it will match either a 'y' at the beginning of the string, a 'yes' or 'n' anywhere in the string, or a 'no' at the end. You need to put parens around the patterns to isolate them from the anchors. Also, I'd prefer an elsif to nested ifs:

      if (/^(y|yes)$/i) { print "YIPPEEE.!!!!\n"; } elsif (/^(n|no)$/i) { print "Begone, evil peon!\n" } else { # Validate user input and return message if invalid print "\n\n=====================================\n"; print "You Have Entered and Incorrect Option\n"; print "Valid Options are [Y/N]\n"; print "=====================================\n\n"; &answer; }

      However, I would much prefer to use a for as a switch case:

      SWITCH: for ($input) { /^(y|yes)$/i && do { print "YIPPEEE.!!!!\n"; last SWITCH; }; /^(n|no)$/i && do { print "Begone, evil peon!\n"; last SWITCH; }; # Validate user input and return message if invalid print "\n\n=====================================\n"; print "You Have Entered an Incorrect Option\n"; print "Valid Options are [Y/N]\n"; print "=====================================\n\n"; &answer; }

      LAI
      :eof
Re: Yes No Validation
by Three (Pilgrim) on Feb 07, 2003 at 14:28 UTC

    I am putting a sub from a program that I wrote called Program Archiver.

    This is a generic question answering subroutine

    Here is the sub and some examples.

    #Useage #Plain yes/no if (stdin_question() =~ /yes/) { #Do something } #Y or N if (stdin_question('y,n','(Y)es (N)o') =~ /y/) { #Do something } #On or off if (stdin_question("on,off","(on or off) :",1) =~ /on/) { #Do somethin +g } #Advanced useage $ret = stdin_question("subdir,manual,none","(subdir, manual, none) :") +; #Test return value and do somthing #--------------------------------------------------------------------- +------------- # STDIN_QUESTION # # If no arguments assume yes and no answers. returns a y or n # # $options Holds the list of valid options. # $text Hold the text to be displayed. # $full_match Tests for full match # #--------------------------------------------------------------------- +------------- sub stdin_question { #Get arguments my ($options,$text,$match) = @_; #Get passed in var #Varables my $input; my $flag = 0; my @valid_options; #Check for defintion of options if (!defined($options)) { $options = "yes,no"; } #Check for defintion of text if (!defined($text)) { $text = "(yes or no) :"; } #Check for defination of match if (!defined($match)) { $match = 0; } #Put options in an arry @valid_options = split/,/, $options; #While no match loop while ($flag ne 1) { #Print out std text print $text; #Read and chomp stdin $input = <stdin>; chomp $input; #Loop through arry for match foreach (@valid_options) { #Test forback match if (/$input/i) { #Test for full match to compare on length if ($match eq 1) { if (length($_) eq length($input)) { $flag = 1; } } else { $flag = 1; } } } } #Return answer return lc $input; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://233215]
Approved by pfaut
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-19 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found