Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Broken Write function? Does anyone have Magic fixer ability?

by ProgrammerJutsu
on Jan 10, 2013 at 23:17 UTC ( #1012755=perlquestion: print w/replies, xml ) Need Help??
ProgrammerJutsu has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Broken Write function? Does anyone have Magic fixer ability?
by davido (Archbishop) on Jan 11, 2013 at 00:26 UTC

    You don't see any problems here?

    $i = 0; my $filename2 = "invaildAddress"; $arrayLength = scalar(@addressSave); print $arrayLength ."\n"; open(IN2 , '>', $filename2); print IN "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);

    First you set $i to 0. Then you open a file with the handle "IN2" for output, but fail to check open for success. Then you print a line to the closed filehandle named "IN". Then you have a while loop that never executes because $i still holds zero, which should never be greater than $addressL. Since the only lines that print to IN2 are inside of a loop that never executes, you get no output. Within your loop at least you're trying to print to the correct filehandle.


    Dave

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Broken Write function? Does anyone have Magic fixer ability?
by dave_the_m (Prior) on Jan 10, 2013 at 23:42 UTC
    So, you call everyone on this site morons, then expect people to help you. You have an interesting perspective on human psychology.

    Dave.

Re: Broken Write function? Does anyone have Magic fixer ability?
by tobyink (Abbot) on Jan 10, 2013 at 23:55 UTC
    open(IN2 , '>', $filename2); ### "IN2" print IN "Invalid Addresses:\n\n\n"; ### "IN" !!!

    (Of course, why you're calling an output file "IN" is another mystery...)

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Broken Write function? Does anyone have Magic fixer ability?
by blue_cowdawg (Monsignor) on Jan 11, 2013 at 15:05 UTC
        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.

    It's comments like that which cause me to heap scorn on your posts. If you were one of my students I'd probably end up failing you for the semester. I won't tell you about formatting (this time) but I will point out several flaws in your programming.

    1. Failure to use the pragma "strict." This probably would have saved you some pain
    2. Lack of error checking (see your "open" calls) to ensure what you want to have happen happens
    Those two issues alone would have me give your programming assignment a failing grade if you were one of my students.

    I have to be honest with you: when I see posts like the one I am critiquing now and others like this one from someone who has been around this Monastery for less than a week and is flame baiting I consider ignoring anything else you post.

    I've been on this site since 2001. Experience has taught me that here in PM as well as in life a bit of humility goes a very long way if you want help from others learning any skill and especially Perl. Tone down your attitude and you'll find this is a great place to hang out and a go to place to learn about Perl. My expectations of my students can apply here:

    1. Do you own work
    2. Write proper code with embedded documentation
    3. If you come to me for help with a programming issue:
      1. Explain what you are trying to accomplish or what you expect to happen
      2. Show me the code. There should be just enough for it to be self contained but don't overwhelm me with your entire source.
      3. Provide me with an error code. Copy/paste if you can.
    4. Be professional/polite. I've been in the field since 1981 and I have personal experience that taught me that bad attitude gets you nowhere.

    The other thing that irritates me are node titles that smack of the author being a smart ass. "Magic fixer ability", really? Did you get that from a Pokemon episode?


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      A reply falls below the community's threshold of quality. You may see it by logging in.
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Broken Write function? Does anyone have Magic fixer ability?
by Your Mother (Bishop) on Jan 11, 2013 at 03:11 UTC

    *You’re a genius.

Re: Broken Write function? Does anyone have Magic fixer ability?
by frozenwithjoy (Priest) on Jan 11, 2013 at 08:01 UTC
    Even if this doesn't solve your two stated problems, the very first thing you need to do is add this to the top (and then deal with any errors/warnings that may arise):
    use strict; use warnings; use autodie; # or give a die option when opening each filehandle
Re: Broken Write function? Does anyone have Magic fixer ability?
by Mr. Muskrat (Canon) on Jan 11, 2013 at 21:42 UTC

    Troll much?

      A troll acts like a jerk to cause trouble and to start flame wars. This guy's not a troll; he's the real deal.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012755]
Approved by rovf
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2018-08-20 12:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (191 votes). Check out past polls.

    Notices?