in reply to Yes No Validation

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

Replies are listed 'Best First'.
Re^2: Yes No Validation
by LAI (Hermit) on Feb 06, 2003 at 20:52 UTC

    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