Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a simple yes or no question (user input)

Status
Not open for further replies.

zitvig

Technical User
Apr 3, 2002
6
MT
hi all,

ok - i'm a perl noob. but remember that helping others makes you feel good;)

i just started experimenting with Perl and I'm trying ask the user for confirmation, so I'm doing the following:

print "\nDo you wish to continue? (Y/n)";
my $ok = <>; # ask for user input
if ($ok == "y" || $ok == "Y" || $ok == ""){
print "you pressed y, Y or enter";
}
elsif($ok == "n" || $ok == "N"){
print "you pressed n or N";
}
else{
print "can't you read!";
}

"yes" would be the default here so I would like to allow the user to just press enter. The problem is any input is taken as a "Y".

maybe i have syntax problems so i simplified the code to:

my $ok = <>; # ask for user input
if ($ok == "y"){
print "you pressed y";
exit;
}
print "you didn't press y";

same thing... any input is taken as a "y".

what am I doing wrong? should i use regular expressions?

i also tried:
if ($ok == m/y/)
same thing - always evaluates as true.

this (=~) seems to work a bit better:
if ($ok =~ m/y/)
y is yes
n (or anything other than y in this case) is no
but ny is yes! (as long as there's a y somewhere in the string it evaluates to true)

best regards,
Mark
 
== is num operator
eq is String operator

If you are doing pattern matching, use the bind operator.

if ($ok =~ m/y/i) { {--> Lower or Upper will work.
do something.
}
elsif ($ok =~ m/n/i) {
do something.
} esle {

}
 
Be careful with $something =~ m/y/ - that will match if there's a 'y' anywhere in the string.

Also, like max1x pointed out, you're using the wrong operator - you should have been getting a warning that said something like "... not numeric in comparison".

Using regexes, you can try using something something like this:
Code:
my $quit = 0;

until ($quit) {
    print "Enter Y|N|Q: ";
    chomp(my $input = <STDIN>);
    
    if ($input =~ /^[Y]?$/i) {      # Match Yy or blank
        print "You win a prize!\n";
    } elsif ($input =~ /^[N]$/i) {  # Match Nn
        print "No prize for you!\n";
    } elsif ($input =~ /^[Q]$/i) {  # Match Qq
        print "Ok, you're done.\n";
        $quit = 1;
    } else {
        print "What? Try again.\n";
    }
}
The character classes aren't necessary, but in this case I think it makes the regexes easier to read.
 
thanx for your reply max1x.

i thought eq was just an equivalent of ==

i had already tried you suggestion in my previous post. the problem with it is that it will evaluate to true as long as there is "y" anywhere in the string - so "NNNNynnn" is taken as a yes.

i tried your eq suggestion as it sounds more promising.
Now someone please tell me why this doesn't work:
my $ok = <>;
my $yes = "y";
if ($ok eq $yes) {
print "YES!";
}

while this one does:
my $ok = "y";
my $yes = "y";
if ($ok eq $yes) {
print "YES!";
}
 
great stuff rharsh! no rocket science but i had only heard of regex up to a few days ago. Now I'll try to slowly figure out what all the codes mean.

thanks both of you for your help.

p.s.
how do you quote code in this forum like rharsh did?
 
Good catch rharsh. Curiousity q, why would u do [] as it's only a single character and not a range?

zitvig:

^ = start of line
$ = end of line.
 
I put a comment about that right after the code I posted - the character classes aren't necessary (the []'s), they're just for readability.
 
input from command line probably needs to be chomped (removes the trailing newline \n):

Code:
chomp(my $ok = <>);
my $yes = 'y';
if ($ok eq $yes) {
   print "YES!";
}
 
yep, Kevin. i discovered chomping as well:)

it's amazing how people from different countries and backgrounds end up doing the same things/thinking the same way... sometimes. whatever.
 
well, perl is perl, regardless of your where you live. So the programming conventions should be the same. [smile]
 
I would (personally):-

1. prompt for user input (chomp it to remove the newline)
2. check to see if the lc (lowercase) of the input is equal to 'yes' or 'y'
3. branch accordingly...

Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top