Connecting Tech Pros Worldwide Forums | Help | Site Map

Trouble with while loop

Newbie
 
Join Date: May 2009
Posts: 1
#1: Jun 5 '09
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

Member
 
Join Date: Jun 2009
Posts: 54
#2: Jun 5 '09

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.  
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#3: Jun 5 '09

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
Member
 
Join Date: Jun 2009
Posts: 54
#4: Jun 6 '09

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)
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,722
#5: Jun 7 '09

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!
Newbie
 
Join Date: Apr 2008
Location: Ireland
Posts: 4
#6: Jun 19 '09

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. }
Member
 
Join Date: Jun 2009
Posts: 54
#7: Jun 19 '09

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