By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,903 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,903 IT Pros & Developers. It's quick & easy.

Trouble with while loop

P: 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
Jun 5 '09 #1
Share this Question
Share on Google+
6 Replies


Expert Mod 100+
P: 589
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.  
Jun 5 '09 #2

KevinADC
Expert 2.5K+
P: 4,059
@RonB
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
Jun 5 '09 #3

Expert Mod 100+
P: 589
@KevinADC
Oh no! Not that...anything but that!! :o)
Jun 5 '09 #4

NeoPa
Expert Mod 15k+
P: 31,530
@RonB
Actually, there's special dispensation for one-liners (I was overridden) ;)

Welcome to Bytes!
Jun 7 '09 #5

P: 4
[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. }
Jun 19 '09 #6

Expert Mod 100+
P: 589
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.  
Jun 19 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.