Trouble with while loop 
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: - sub addVoter{
-
open(AV,">>voters.txt");
-
print "What is your name (first and last)? ";
-
chomp($addName=<STDIN>);
-
print AV "$addName:";
-
print "What is your SSN? ";
-
chomp($addSsn=<STDIN>);
-
$int=0;
-
while($int==0){
-
if ($addSsn =~ /\d{3}-\d\{2}-\d{4} /){
-
print AV "$addSsn:";
-
$int=1;
-
}
-
else{
-
print "ERROR: please type SSN again in this format XXX-XX-XXXX.";
-
chomp($addSsn=<STDIN>);
-
}
-
}
-
}
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.
| 
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:
Last edited by numberwhun; June 7th, 2009 at 07:49 PM.
Reason: Please use code tags!
| 
June 5th, 2009, 10:45 PM
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,097
| | | re: Trouble with while loop Quote:
Originally Posted by RonB 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
| 
June 6th, 2009, 12:16 AM
| | Member | | Join Date: Jun 2009
Posts: 43
| | | re: Trouble with while loop Quote:
Originally Posted by KevinADC 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)
| 
June 7th, 2009, 12:18 PM
|  | Administrator | | Join Date: Oct 2006 Location: London - UK
Posts: 15,450
Provided Answers: 56 | | | re: Trouble with while loop Quote:
Originally Posted by RonB 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!
| 
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 - sub addVoter{
-
open(AV,">>voters.txt");
-
my $addSsn="";
-
print "What is your name (first and last)? ";
-
chomp($addName=<STDIN>);
-
print AV "$addName:";
-
my $checked=0;
-
while (($addSsn !~ /^\d{3}-\d\{2}-\d{4}$/){
-
print "What is your SSN? ";
-
chomp($addSsn=<STDIN>);;
-
$int=1;
-
print "ERROR: please type SSN again in this format XXX-XX-XXXX." if ($checked==1);
-
$checked=1;
-
}
-
}
| 
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: -
sub addVoter{
-
-
open my $voters_FH,'>>', 'voters.txt'
-
or die "failed to open 'voters.txt' $!";
-
-
print "What is your name (first and last)? ";
-
chomp(my $addName=<STDIN>);
-
-
my $addSsn;
-
do {
-
print "Please type your SSN in this format XXX-XX-XXXX ";
-
chomp($addSsn=<STDIN>);
-
} while ( $addSsn !~ /^\d{3}-\d{2}-\d{4}$/ );
-
-
print $voters_FH join(':', $addName, $addSsn), "\n";
-
}
-
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|