Im writing code for checking if a database of addresses are valid. i have the code to save the database into scalars using regex. and the submission to the web site but I'm getting some errors.
I have speculated where the code may be broken because im not getting any values to print to my addressUpdate.txt also im new to these functions so any tips you have help.
If someone could review the code to help me fix it, i would be very grateful. The senior programmer that im interning with is busy due to tax season. Thanks =)
INDEX:
Code
Part 1: Test.pl
Part 2: addresses.txt
End
Part 1:
#!/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 = "";
#read in
my $filename = ('addresses.txt');
my $txtContent = "";
open(IN, '<', $filename);
while(my $line = <IN>){
$txtContent .= $line;
}
close(IN);
#split
my @address = split(m/[\r]+/, $txtContent);
my $addressL = scalar(@address);
print $addressL . "\n";
#Save Address Elements to scalars
while($i < $addressL){
$address = @address[$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*\S*\d+[\d\s\w.,#]+)/){
my $addressNum = $1;
$printADD = $addressNum;
}
elsif($address =~ /([\w\s]+)/i){
$data = $1;
$data1 .= $data;
}
if($address =~ /(suite\s\d+)/i){
$suite = $1;
$printSuite = $suite;
}
if(($printADD =~ /\d+/) && ($printSuite =~ /suite\s\d+/i)){
$printADD = "";
}
#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]+/+/){};
print $printPO;
print "\n";
print $printADD;
print $printSuite;
print "\n";
print $city1;
print "\n";
print $state1;
print "\n";
print $zip1;
print "\n";
my %address =(
'City' => "$city1",
'State' => "$state1",
'Zip' => "$zip1",
'Address' => "$printADD",
'PoBox' => "$printPO",
'Suite' => "$printSuite");
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);
#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/[+]+/ /){};
print IN $addressSave[$i]->{City} . "\n"; ## BROKEN?
print IN $addressSave[$i]->{State} . "\n";
print IN $addressSave[$i]->{Zip} . "\n";
print IN $addressSave[$i]->{Address} . "\n";
print IN $addressSave[$i]->{PoBox} . "\n";
print IN $addressSave[$i]->{Suite} . "\n";
$i++;
}
close(IN);
####################################################SUBS##############
+######################################SUBS
sub form{
my ($addressSave) = @_;
my $i = 0;
my $arrayLength = scalar(@{$addressSave});
while($i < $arrayLength){
my $city = $addressSave->[$i]->{City}; #Broken here?
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};
Dumper(\@{$addressSave}->[$i]);
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; ## BROKEN?
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 = ($response->content);
if($htmlResult =~ m/<div class='Titresultablerr'>Address NOT V
+erified/){
print 'Error on :' . $addressSave[$i];
delete($addressSave[$i]);
print "\n";
}
else{
print "Address Valid\n";
}
$i++;
}
}
sub save{
my ($address, $addressSave) = @_;
push(@{$addressSave}, %{$address});
}
Part 2: 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
Idaho Idaho Department of Commerce, Travel, Leisure and Tourism
700 West State St.
PO Box 83720
Boise, ID 83720-0093
Illinois Illinois Dept. of Commerce and Community Affairs
620 E. Adams
Springfield, IL 62701
Indiana Indiana Department of Tourism
One North Capitol Suite 700
Indianapolis, IN 46204
Iowa Iowa Dept. of Economic Development
200 East Grand Ave.
Des Moines, IA 50309
Kansas Kansas Dept. of Commerce
Travel And Tourism Div.
1000 S.W. Jackson Street
Suite 100
Topeka, KS 66612
Kentucky Kentucky Department of Travel
500 Mero St. #2200
Frankfurt, KY 40601
Louisiana Louisiana Dept. of Culture, Recreation and Tourism
PO Box 94291
Baton Rouge, LA 70804-9291
Maine Maine Office of Tourism
#59 State House Station
Augusta, ME 04333-0059
Maryland Maryland Office of Tourism Development
217 Redwood St
Baltimore, MD 21202
Massachusetts Massachusetts Office of Travel and Tourism
10 Park Plaza
Suite 4510
Boston, MA 02116
Michigan Michigan Economic Development
300 N. Washington Sq.
Lansing, MI 48913
Minnesota Minnesota Office of Tourism
500 Metro Square
121-7th PL East
St. Paul, MN 55101
Mississippi Mississippi Dept. of Economic and Community Development
Division of Tourism Development
PO Box 849
Jackson, MS 39205
Missouri Missouri Division of Tourism
PO Box 1055
Jefferson City, MO 65102
Montana Montana Department of Commerce
1424 9th Ave.
PO Box 200533
Helena, MT 59620-0533
Nebraska Nebraska Tourism Office
PO Box 98907
Dept. 9INT
Lincoln, NE 68509-8907
Nevada Nevada Commission on Tourism
401 North Carson St.
Carson City, NV 89701
New Hampshire New Hampshire Office of Tourism
PO Box 1856
Concord, NH 03302
New Jersey New Jersey Commerce and Economic Growth Commission
PO Box 820
20 W. State St.
Trenton, NJ 08625
New Mexico New Mexico Department of Tourism
491 Old Santa Fe Trail
PO Box 20002
Santa Fe, NM 87501
New York New York Department of Tourism
PO Box 2603
Albany, NY 12220-0603
North Carolina North Carolina Department of Commerce
301 N. Wilmington St.
Raleigh, NC 27601
North Dakota North Dakota Tourism
Liberty Memorial Building
604 East Boulevard
Bismark, ND 58505-0825
Ohio Ohio Department of Commerce
77 South High St
23rd Floor
Columbus, OH 43266-0544
Oklahoma Oklahoma Department of Tourism and Recreation
20 N. Robinson Avenue, 6th Floor
PO Box 52002
Oklahoma City, OK 73152-2002
Oregon Oregon Toursim Commission
775 Summer St, NE
Salem, OR 97310
Pennsylvania Pennsylvania Tourism Office
Dept. of Community & Economic Development
4th Floor, Commonwealth Keystone Building
400 North Street
Harrisburg, PA 17120
Rhode Island Rhode Island Economic Development Corporation
Tourism Division
One West Exchange St.
Providence, RI 02903
South Carolina South Carolina Department of Parks, Recreation and T
+ourism
1205 Pendleton St
Suite 106
Columbia, SC 29201
South Dakota South Dakota Department of Tourism
711 East Wells Ave.
c/o 500 East Capitol Ave.
Pierre, SD 57501-5070
Tennessee Tennessee Department of Tourist Development
Rachel Jackson State Office Building
320 Sixth Ave., 5th Floor
Nashville, TN 37243
Texas Texas Dept. of Economic Development
PO Box 12728
Austin, TX 78711
Utah Utah Travel Council
Council Hall
Capitol Hill
Salt Lake City, UT 84114
Vermont Vermont Agency of Commerce and Community Development
Department of Tourism and Marketing
6 Baldwin St., Drawer 33
Montpelier, VT 05603-1301
Virginia Virginia Tourism Corporation
901 E. Byrd St.
Richmond, VA 23219
Washington Washington State Community, Trade and Economic Developme
+nt
906 Coumbia St. SW
PO Box 48300
Olympia, WA 98504-8300
West Virginia West Virginia Division of Tourism
2101 Washington St., E.
Charleston, WV 25305
Wisconsin Wisconsin Department of Tourism
201 W. Washington Ave.
PO Box 8690
Madison, WI 53708-8690
Wyoming Wyoming Travel & Tourism
I-25 at College Dr.
Cheyenne, WY 82002