473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Trouble with while loop

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
6 1867
RonB
589 Expert Mod 512MB
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
4,059 Expert 2GB
@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
RonB
589 Expert Mod 512MB
@KevinADC
Oh no! Not that...anything but that!! :o)
Jun 5 '09 #4
NeoPa
32,556 Expert Mod 16PB
@RonB
Actually, there's special dispensation for one-liners (I was overridden) ;)

Welcome to Bytes!
Jun 7 '09 #5
[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
RonB
589 Expert Mod 512MB
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

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

Similar topics

1
by: Andreas Paasch | last post by:
Hi there, below a little code snippet that causes me some headaches. I had a similar one working fine, until I needed to redo my code on this one page entirely. Now my problem is, that the test...
2
by: apec | last post by:
Dear All, I'm having trouble getting my c++ code to work (below). Any suggestions as to what could be the problem? I think it has to do with the while loop, since the program ends before command...
1
by: teddarr | last post by:
I'm having trouble reading the first 2 lines of data in an external file. I am supposed to use a while loop to read the first 2 lines of an external file that contains several random integers. I...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
10
by: Hendri Adriaens | last post by:
Hi, I'm trying to automate the creation of an excel file via COM. I copied my code below. I read many articles about how to release the COM objects that I create. The code below runs just fine...
3
by: bmerlover | last post by:
I believe my problem lies inside the while loop. When I click the play button on the gui app, it goes inside the while loop, reads the file and calls the necessary function to do what it needs to do....
0
by: bmerlover | last post by:
This code makes sense to me, I'm just having trouble trying to understand why it doesn't work correctly. This is a GUI APP. When the Play button is Clicked, the play_Click(System::Object * sender,...
4
by: Villanmac | last post by:
First up thanks to the guys who pointed out my error last week. Now I was wondering If I could get some help with this problem because again I have fallen into trouble. Basically im using the...
2
by: cmb3587 | last post by:
I am having a problem with the validation of the account number and password. The beginning of the program asks for users account # then pwd. The program is then supposed to go to a checkID...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.