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

Good morning most gracious monks, I need help with the following: I need to be able to ask a simple question and if they do not respond yes or y then I want the program to end. If they do answer yes or y I would like the program to continue and ask them if they are sure and if they say yes continue on to the next thing or exit the program. thanks bob ------------------------------------------------------------
next; } print "Do you want to submit Job A? "; chomp ($question = <STDIN>); # Submitting Job A unless ($question=~ /yes||y/gi) { die"Goodbye\n";} else { print "Are You Sure? "; } chomp ($question2= <STDIN>); if ($question2=~ /yes||y/gi) { print "You said YES !\n"; } else { exit }

Replies are listed 'Best First'.
Re: I think I'm close...
by japhy (Canon) on Mar 01, 2002 at 16:52 UTC
    You have confused Perl's logical OR operator || with the regex's alternation token, |. And why do you have the /g modifier in your regex?

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Jeff is right, use the single |, this should do what you want, will die if the answer is not Y or Yes, otherwise just carries on. Just make sure you use warnings and strict
      print "Do you want to submit Job A? "; chomp (my $question = <STDIN>); die "Goodbye\n" unless $question=~ /(yes|y)/i; print "Are You Sure? "; chomp (my $question2= <STDIN>); die "Goodbye\n" unless $question2 =~ /(yes|y)/i; print "Everything good\n";
      Hope this helps ...
      what does the g do anyway (i forget)? I think I must of swiped this from somewhere else (another piece of code) Thanks it is working now bob
        The /g is Perl's global match modifier. In this case, you're only looking for a single match, so you don't need it.

        I would also suggest taking the approach VSarkiss uses below and anchoring your regex:

        	if ($question =~ /^y/i) ...
        
        works great for all concerned, while this:
        	if ($question =~ /(yes|y)/i) ...
        
        not only matches, "yes" and "y", but also matches "maybe", "not necessarily", "no way", "definitely not", and "/me rolls eyes".

        -- grummerX

        The g matches globally(all occurances). In a list context, it returns the list of substrings matched by capturing parentheses(if no capturing parens, it returns a list of all matched strings).
Re: I think I'm close...
by VSarkiss (Monsignor) on Mar 01, 2002 at 17:30 UTC

    If you're going to demand the string be "yes" or "y", there's no need to use a regex. Just test for string equality: if ($question eq 'yes' || $question eq 'y') ... Possibly that's what got you confused about using the two vertical bars instead of one.

    On the other hand, if you're going to be liberal in what the program accepts, then a regex works great. But you can make it simpler: if ($question =~ /^y/i) ...That will accept "yes", "y", "yeah", "yep", and "yoohoo!" (and their uppercase counterparts ;-).

    HTH