This is an address verifier
Cant write to my file "invalidaddress"
Hit me with knowledge!
Despite my lack of faith.
So im getting 2 errors. 1) the values aren't printing to invalidAddress.txt, and 2) the regular expression that searches the html source code fails to find when there is an error.
I have been looking at this code for so long that i cant even tell whats wrong with it.
Look right before the subs start. There is: my $filename2
The values come from within my "form" subroutine.
Thanks
fyi - the addresses are mostly good addresses but they are set to auto fail.
#!/usr/bin/perl
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Request::Common;
use Data::Dumper;
#declare
my @addressSave = ();
my $printCSZ = "";
my $printPO = "";
my $printADD = "";
my $printSuite = "";
my $i = 0;
my $poBoX = "";
my $data = "";
my $data1 = "";
my $city = "";
my $city1 = "";
my $state = "";
my $state1 = "";
my $zip = "";
my $zip1 = "";
my $address = "";
my @invalidAdd = ();
my $arrayLength = "";
#read in
my $filename = ('addresses.txt');
my $txtContent = "";
open(IN, '<', $filename);
while(my $line = <IN>){
$txtContent .= $line;
}
close(IN);
#split
my @addresses = split(m/[\r]+/, $txtContent);
my $addressL = scalar(@addresses);
print $addressL . "\n";
#Save Address Elements to scalars
while($i < $addressL){
$address = @addresses[$i];
if($address =~ /(\w+\s*\w*)\S\s(\w\w)\s(\d+)-*\d*/){
$state = $2;
$city = $1;
$zip = $3;
$city1 = $city;
$state1 = $state;
$zip1 = $zip;
}
elsif($address =~ /(po\sbox\s\d+)/i){
$poBox = $1;
$printPO = $poBox
}
elsif($address =~ /(\w*\D*\d+[\d\s\w.,#]+)/){
my $addressNum = $1;
$printADD .= $addressNum . "\n";
}
elsif($address =~ /(\w+[\w\s]+)/i){
$data = "$1\n";
$data1 .= $data;
}
if($address =~ /(suite\s\d+)/i){
$suite = $1;
$printSuite = $suite;
}
if(($address =~ /(suite\s\d+)/i) && ($printSuite =~ /(suite\s\d+)/
+i)){
$printSuite = "";
}
#Run if there are values
if($city1 eq ""){
}
else{
while($city1 =~ s/[\s]+/+/){};
while($state1 =~ s/[\s]+/+/){};
while($zip1 =~ s/[\s]+/+/){};
while($printPO =~ s/[\s]+/+/){};
while($printADD =~ s/[\s]+/+/){};
while($printSuite =~ s/[\s]+/+/){};
while($data1 =~ s/[\s]+/+/){};
my %address =(
'City' => "$city1",
'State' => "$state1",
'Zip' => "$zip1",
'Address' => "$printADD",
'PoBox' => "$printPO",
'Suite' => "$printSuite",
'MiscData' => "$data1");
save(\%address, \@addressSave);
#TASK
#### Save scalar values to a hash
#### save hash values to an array
#### post hash values to USPS
#### Verify
#### RED flag if address is not real
#### Separate bad values from good values
$printPO = "";
$printADD = "";
$printSuite = "";
$city1= "";
$state1 = "";
$zip1 = "";
$data1 = "";
}
$i++;
}
#Verify if Values are correct
form(\@addressSave, \@invalidAdd);
#Write updated Database
$filename1 = ('addresssUpdate.txt');
open(IN, '>', $filename1);
$i = 0;
my $arrayLength = scalar(@addressSave);
while($i < $arrayLength){
while($addressSave[$i]->{City} =~ s/[+]+/ /){};
while($addressSave[$i]->{State} =~ s/[+]+/ /){};
while($addressSave[$i]->{Zip} =~ s/[+]+/ /){};
while($addressSave[$i]->{Address} =~ s/[+]+/ /){};
while($addressSave[$i]->{PoBox} =~ s/[+]+/ /){};
while($addressSave[$i]->{Suite} =~ s/[+]+/ /){};
while($addressSave[$i]->{MiscData} =~ s/[+]+/ /){};
print IN "City = " . $addressSave[$i]->{City} . "\n";
print IN "State = " . $addressSave[$i]->{State} . "\n";
print IN "Zip = " . $addressSave[$i]->{Zip} . "\n";
print IN "Address = " . $addressSave[$i]->{Address} . "\n";
print IN "Po Box = " . $addressSave[$i]->{PoBox} . "\n";
print IN "Suite = " . $addressSave[$i]->{Suite} . "\n";
print IN "Misc data = " . $addressSave[$i]->{MiscData} . "\n\n
+";
$i++;
}
close(IN);
####Write invalid add
$i = 0;
my $filename2 = "invaildAddress";
$arrayLength = scalar(@addressSave);
open(IN2 , '>', $filename2);
print IN2 "Invalid Addresses:\n\n\n";
while($i < $addressL){
##########BROKE?
print IN2 "City = " . $invalidAdd[$i]->{City} . "\n";
print IN2 "State = " . $invalidAdd[$i]->{State} . "\n";
print IN2 "Zip = " . $invalidAdd[$i]->{Zip} . "\n";
print IN2 "Address = " . $invalidAdd[$i]->{Address} . "\n";
print IN2 "Po Box = " . $invalidAdd[$i]->{PoBox} . "\n";
print IN2 "Suite = " . $invalidAdd[$i]->{Suite} . "\n";
print IN2 "Misc data = " . $invalidAdd[$i]->{MiscData} . "\n\n";
$i++;
}
close(IN2);
####################################################SUBS##############
+######################################SUBS
sub form{
my ($addressSave, $invalidAdd) = @_;
my $i = 0;
my $arrayLength = scalar(@{$addressSave});
while($i < $arrayLength){
my $city = $addressSave->[$i]->{City};
my $state = $addressSave->[$i]->{State};
my $zip = $addressSave->[$i]->{Zip};
my $add = $addressSave->[$i]->{Address};
my $po = $addressSave->[$i]->{PoBox};
my $suite = $addressSave->[$i]->{Suite};
print "\n\n";
my $url = "http://www.melissadata.com/lookups/addressverify.a
+sp";
my $agent = new LWP::UserAgent;
my $newPost = new HTTP::Request('GET', $url);
my $currentZipCode;
$newPost->content_type('application/x-www-form-urlencoded');
my $post = "name=&Company=&Address=$add+$po&city=$city&state=$
+state&zip=$zip" . $currentZipCode;
print "post = " . $post . "\n";
$newPost->content($post);
my $response = $agent->request($newPost);
if ($response->is_success) {
print "response success\n";
}
##--Handle successful post data
my $htmlResult = "Address NOT Verified";#($response->content);
if($htmlResult =~ m/Address\sNOT\sVerified/i){
print "address invalid";
$addressSave->[$i] = $invalidAdd->[$i];
$addressSave->[$i] = ();
print "\n";
}
elsif($htmlResult =~ m/is\srequired\sto\sprocess\sthe\saddress
+/i){
print "address invalid\n";
$addressSave->[$i] = $invalidAdd->[$i];
$addressSave->[$i] = ();
print "\n";
}
else{
print "Address Valid\n";
}
$i++;
}
}
sub save{
my ($address, $addressSave) = @_;
push(@{$addressSave}, $address);
}
Please dont respond if you want to tell me about formatting, or if you want to tell me this is a continuation post or if you dont have anything constructive to input. Unless you want to tell me im a genius.
Thanks,
ProgrammerJutsu
addresses.txt
Alabama Alabama Bureau of Tourism & Travel
PO Box 4927
Montgomery, AL 36103
Alaska Alaska Division of Tourism
PO Box 110801
Juneau, AK 99811-0801
Arizona Arizona Office of Tourism
1110 W. Washington Street, Suite 155
Phoenix, AZ 85007
Arkansas Arkansas Department of Parks and Tourism
One Capitol Mall
Little Rock, AR 72201
California California Division of Tourism
PO Box 1499
Dept TIA
Sacramento, CA 95812
Colorado Colorado Tourism Office
1625 Broadway Suite 2700
Denver, CO 80202
Connecticut Connecticut Commission on Culture & Tourism
One Financial Plaza
755 Main Street
Hartford, CT 06103
Delaware Delaware Tourism Office
99 Kings Highway
PO Box 1401
Dover, DE 19903
Florida Florida Office of Tourism
PO Box 1100
Tallahassee, FL 32302
Georgia Georgia Department of Economic Development
75 Fifth Street, N.W., Suite 1200
Atlanta, GA 30308
Hawaii Hawaii Department of Business, Economic Development and Tour
+ism
PO Box 2359
Honolulu, HI 96804
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.