Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

While loop for reading text file 1

Status
Not open for further replies.

bobcubsfan

Programmer
Oct 10, 2007
4
US
I have a file of zip codes. When the form contains a good zip code, my program finds it along with the city and state. If the zip is bad and is not found in the file, my program does not finish. It hangs the sever. I have tried eof as well as some other programming, but nothing seems to work.

Here is the code:

#!/usr/bin/perl


if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
} else {
print "Content-type: text/html\n\n";
print "<P>Use Post or Get";
}

foreach $pair (@pairs) {
($key, $value) = split (/=/, $pair);
$key =~ tr/+/ /;
$key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$value =~s/<!--(.|\n)*-->//g;

if ($formdata{$key}) {
$formdata{$key} .= ", $value";
} else {
$formdata{$key} = $value;
}
}

print "Content-type: text/html\n\n";


$my_name=$formdata{'Name'};
$my_address1=$formdata{'address1'};
$my_address2=$formdata{'address2'};
$my_phone=$formdata{'phone'};
$my_email=$formdata{'email'};

$my_zip=$formdata{'zip'};
$my_zip=substr($my_zip,0,5);


$found_flag = "F";

$my_state ="";

$my_city = "";


open (ZC, "ZIP_CODES.txt") ;


while ($found_flag = "F") {

$mystuff = <ZC>;

if ($mystuff eq "notfound") {

last;
}


if ($my_zip eq substr($mystuff,0,5)) {

$my_state = substr($mystuff,5,2);

$my_city = substr($mystuff,7,);




$found_flag = "T";

last;

}


}


close(ZC);
 
Ignoring other problems with your script....

does the file actually contain the line/string "notfound"? If not the condition will never be true and the "while" loop will never exit. It seems that condition is not even necessary:

Code:
print "Content-type: text/html\n\n";
$my_name=$formdata{'Name'};
$my_address1=$formdata{'address1'};
$my_address2=$formdata{'address2'};
$my_phone=$formdata{'phone'};
$my_email=$formdata{'email'};
$my_zip=$formdata{'zip'};
$my_zip=substr($my_zip,0,5);
$found_flag = "F";
$my_state ="";
$my_city = "";

open (ZC, "ZIP_CODES.txt") ;
while (my $mystuff = <ZC>) {
   if ($my_zip eq substr($mystuff,0,5)) {
      $my_state = substr($mystuff,5,2);
      $my_city = substr($mystuff,7,);
      $found_flag = "T";
      last;
   }
}
if ($found_flag eq 'F') {
    print "ZIP code not found\n";
}




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Yes it does contain that because I could not think of another way to exit the loop. However, it does not work.
 
Does the code I posted work for you? How is the file formatted?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
The file is a simple text file with each line like this:


92158CASan Diego

In the statement you wrote

while (my $mystuff = <ZC>) {

I have not used the syntax "my $mystuff = <ZC>"

I will have to do some reading to understand what it is doing.

Thanks again for your help.
 
Thanks for my 400th star [roll2]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ooh. We are not worthy... [smile]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Show off!! [2thumbsup]

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top