LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-06-2009, 02:54 PM   #1
don_wombat_73
Member
 
Registered: Sep 2005
Posts: 60

Rep: Reputation: 15
Unhappy Stay in a Perl loop with question not answered correctly


I am quite confused by this simple perl loop. I need to have a prompt to ask for a number. If the Number is higher or lower than a specific range, it will reply with a "Sorry, my bad" answer and loop back to the question. Once the number falls between the range, it will let the user out of the loop by incrementing the $COUNT variable.
Here is my current attempt...


Code:
#!/usr/bin/perl
$COUNT = 1;
if ($COUNT = 1)
  {
    print "What is the number?  ";
    $NUM1 = <STDIN> ;
    chomp ($NUM1);
  }
elsif
{
$OCT1_COUNT++;
}
else (($NUM1 < 1) && ($NUM1 > 10))
{
print "That number is not between 1 and 10.\n";
}
What am I doing wrong?!?
 
Old 10-06-2009, 03:17 PM   #2
don_wombat_73
Member
 
Registered: Sep 2005
Posts: 60

Original Poster
Rep: Reputation: 15
[SOLVED] after some thought....

<p>I used the following to solve my problem after I had a moment to think about it.</p>


Code:
$COUNT = 1;
while ($COUNT = 1)
{
  print "What is the number?  ";
  $NUM1 = <STDIN> ;
    if (($NUM1 < 1) || ($NUM1 > 10))
      {
	print "That number is not between 1 and 10.\n";
      }
    else
      {
	chomp ($NUM1);
	$NUM1_COUNT++;
      }
}
 
Old 10-06-2009, 04:13 PM   #3
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,991

Rep: Reputation: 76
You probably want to chomp() the number before using it in a numerical test?
 
Old 10-06-2009, 07:15 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Yep, always chomp() before using an input value or file rec.

Also, change the top of your prog to be

Code:
#!/usr/bin/perl -w
use strict;
Those additions (warnings & strict) will save you a lot of grief later. Think of them like the warnings you get from a compiler.
In fact, Perl is compiled on the fly :
http://www.perl.com/doc/FMTEYEWTK/comp-vs-interp.html

Never use a program until it's warning/error free. A way to test without running is

perl -wc yourprog.pl

You'll find these links invaluable:
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials
 
  


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
need a sendmail question (what is relaying) answered extendedping Linux - Networking 1 03-19-2008 08:29 AM
noob question i need answered cooldudejz Ubuntu 4 09-21-2005 08:44 PM
Another LILO Graphic Question (never answered?) DreameR-X Slackware 2 12-17-2004 04:45 AM
Dare you answer the question, that cannot be answered! Ephemeral Linux - Wireless Networking 8 08-29-2004 07:16 PM
NEWBIE!!! needs question answered microsoft/linux Slackware 15 05-13-2004 09:57 PM


All times are GMT -5. The time now is 06:11 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration