Connecting Tech Pros Worldwide Help | Site Map

Trouble with while loop

  #1  
Old June 5th, 2009, 08:19 AM
Newbie
 
Join Date: May 2009
Posts: 1
hello people,

I am working on a function to add to a file with verifications to check. I am having a problem in my while loop. I am telling the user to input a SSN(fake of course) and if its not in the XXX-XX-XXXX format then the while loop will send an error and they would have to input their SSN again. The problem is even if it the SSN is in the right format it still is going to the error message. Here is my code:
Expand|Select|Wrap|Line Numbers
  1. sub addVoter{
  2.         open(AV,">>voters.txt");
  3.         print "What is your name (first and last)? ";
  4.         chomp($addName=<STDIN>);
  5.                 print AV "$addName:";
  6.         print "What is your SSN? ";
  7.         chomp($addSsn=<STDIN>);
  8.         $int=0;
  9.         while($int==0){
  10.                 if ($addSsn =~ /\d{3}-\d\{2}-\d{4} /){
  11.                         print AV "$addSsn:";
  12.                         $int=1;
  13.                 }
  14.                 else{
  15.                         print "ERROR: please type SSN again in this format XXX-XX-XXXX.";
  16.                         chomp($addSsn=<STDIN>);
  17.                 }
  18.         }
  19. }
Any help is greatly appreciated. Thanks everyone!

-Trev

Last edited by NeoPa; June 7th, 2009 at 12:16 PM. Reason: Please use the [CODE] tags provided.
  #2  
Old June 5th, 2009, 03:40 PM
Member
 
Join Date: Jun 2009
Posts: 43

re: Trouble with while loop


Your regex is not doing what you think.

Try changing it to this:
Expand|Select|Wrap|Line Numbers
  1. /^\d{3}-\d{2}-\d{4}$/
  2.  

Last edited by numberwhun; June 7th, 2009 at 07:49 PM. Reason: Please use code tags!
  #3  
Old June 5th, 2009, 10:45 PM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,097

re: Trouble with while loop


Quote:
Originally Posted by RonB View Post
Your regex is not doing what you think.

Try changing it to this:
/^\d{3}-\d{2}-\d{4}$/
Please use the code tags when posting code. This is your only warning. Next time you will be sentenced to a life of answering all of kumars posts on perlguru.com
  #4  
Old June 6th, 2009, 12:16 AM
Member
 
Join Date: Jun 2009
Posts: 43

re: Trouble with while loop


Quote:
Originally Posted by KevinADC View Post
Please use the code tags when posting code. This is your only warning. Next time you will be sentenced to a life of answering all of kumars posts on perlguru.com
Oh no! Not that...anything but that!! :o)
  #5  
Old June 7th, 2009, 12:18 PM
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,450
Provided Answers: 56

re: Trouble with while loop


Quote:
Originally Posted by RonB View Post
Your regex is not doing what you think.

Try changing it to this:
/^\d{3}-\d{2}-\d{4}$/
Actually, there's special dispensation for one-liners (I was overridden) ;)

Welcome to Bytes!
  #6  
Old June 19th, 2009, 01:42 PM
Newbie
 
Join Date: Apr 2008
Location: Ireland
Posts: 4

re: Trouble with while loop


[quote=TrevS17;3492233]hello people,
No need for the $int flag
Expand|Select|Wrap|Line Numbers
  1. sub addVoter{
  2.         open(AV,">>voters.txt");
  3.         my $addSsn="";
  4.         print "What is your name (first and last)? ";
  5.         chomp($addName=<STDIN>);
  6.         print AV "$addName:";
  7.         my $checked=0;
  8.         while (($addSsn !~ /^\d{3}-\d\{2}-\d{4}$/){
  9.                 print "What is your SSN? ";
  10.                 chomp($addSsn=<STDIN>);;
  11.                 $int=1;
  12.                 print "ERROR: please type SSN again in this format XXX-XX-XXXX." if ($checked==1);
  13.                $checked=1;
  14.         }
  15. }
  #7  
Old June 19th, 2009, 02:18 PM
Member
 
Join Date: Jun 2009
Posts: 43

re: Trouble with while loop


Quote:
No need for the $int flag
That is true, but you left it in anyway and there's also no need for $checked.

There are several other issues; 1) no error handling on the open call, 2) use of a bareword instead of a lexical var for the filehandle, 3) syntax error on the while loop initialization, 4) the regex wasn't corrected.

Try this:
Expand|Select|Wrap|Line Numbers
  1. sub addVoter{
  2.  
  3.     open my $voters_FH,'>>', 'voters.txt'
  4.       or die "failed to open 'voters.txt' $!";
  5.  
  6.     print "What is your name (first and last)? ";
  7.     chomp(my $addName=<STDIN>);
  8.  
  9.     my $addSsn;
  10.     do {
  11.         print "Please type your SSN in this format XXX-XX-XXXX ";
  12.         chomp($addSsn=<STDIN>);
  13.     } while ( $addSsn !~ /^\d{3}-\d{2}-\d{4}$/ );
  14.  
  15.     print $voters_FH join(':', $addName, $addSsn), "\n";
  16. }
  17.  
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble with Algorithm? bmerlover answers 0 September 11th, 2007 01:14 AM
Trouble with while loop compsci answers 2 February 14th, 2007 08:19 PM
trouble with external file usage teddarr answers 1 October 12th, 2006 05:25 AM
trouble with c++ while loop ? apec answers 2 May 18th, 2006 10:37 PM