LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-03-2010, 06:17 PM   #1
oinker
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Rep: Reputation: 0
Question Regular expression doesnt work in an elsif statement?


I was doing an exercise on Learning Perl, 3rd edition. (exercise chapter 10 btw) The problem asks to create a program that generates a random number and asks the user to guess. It should tell the user if the guess is lower or higher and exit if the user types either exit or quit.

My code is the following:

Code:
#! /usr/bin/perl
$correct = int(1+rand 100);
while(true){
chomp($lines = <STDIN>);
    if ($lines == $correct){
    print"You did it boy! \n";
    exit;}
    elsif($lines < $correct){
        print"The number is bigger..\n";}
    elsif($lines > $correct){
        print"The number is smaller..\n";}
    elsif($lines =~ /quit|exit|^\s*$/i) {
    exit;}
}
Their code is:

Code:
#! /usr/bin/perl

 my $secret = int(1 + rand 100);

         while (1) {
           print "Please enter a guess from 1 to 100: ";
           chomp(my $guess = <STDIN>);
           if ($guess =~ /quit|exit|^\s*$/i) {
        print "Sorry you gave up. The number was $secret.\n";
        last;
      } elsif ($guess < $secret) {
        print "Too small. Try again!\n";
      } elsif ($guess == $secret) {
        print "That was it!\n";
        last;
      } else {
        print "Too large. Try again!\n";
      }
}
When i type quit or exit it doesnt work.. what gives??? im confused.

Thanks!
 
Old 02-03-2010, 06:33 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 59
The order of the comparisons in "their code" is done for a reason. The 4 cases to consider, as you know, are:
1) guess == number
2) guess < number
3) guess > number
4) guess == "quit"/"exit".

Lets trace through an example. Say number is 5 and we guess 4. 1) isnt true, so we check the next condition. Now, since guess is not equal to number, either guess is less than number or greater than number. That is, there is no other possibility! Since 4 < 5 is true, it asks for another number. Lets say we input 6. Then 1) is false, 2) is false which means there is only one possibility, and that is that 3) is true, so it asks for another number. Now, say we enter "exit" or "quit". 1) is obviously not true, so we check 2). Depending on how strings are compared to numbers in Perl (or their order in ASCII) either 2) or 3) will be true. That is, control never reaches 4).

To reiterate, since the expected input is a number, there are always only 3 possibilities: ==, <, >, and exactly one of them will be true. So, again, it is not possible for control to reach the if statement in 4). So the order of checking is important. You must check conditions 1) and 4) first (so either 1) then 4), or 4) then 1), it shouldnt matter). The order of conditions 2) and 3) do not matter.
 
1 members found this post helpful.
Old 02-04-2010, 02:33 PM   #3
oinker
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Oh! but of course!!! brilliant, I keep thinking but I couldn't figure it out. Wonderful!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expression (.*?) uttam_h Programming 6 05-30-2008 06:45 PM
Need help with a regular expression abefroman Programming 2 05-28-2008 10:17 PM
Regular Expression msvinaykumar Programming 2 08-14-2006 09:48 AM
Do we have regular expression in C++ ? indian Programming 4 03-06-2006 10:54 AM
Need help with regular expression aecaudel Programming 6 11-04-2005 06:28 AM


All times are GMT -5. The time now is 02:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration