Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
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

Comment on Broken Write function? Does anyone have Magic fixer ability?
Select or Download Code
Re: Broken Write function? Does anyone have Magic fixer ability?
by dave_the_m (Parson) 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'
      it worked the first time =D, I still don't see the error...
      will changing the name really matter?

        "I still don't see the error..."

        You are opening one file handle (called "IN2") and then trying to print to another file handle (called "IN"). They are two different handles.

        Imagine that file handles are closed doors. You open one door, but try to walk through another door. Does that work? If you are in any way unsure, find a room with two doors and try it.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        No, naming doesn't matter at all. You just name file handles because stuff looks way cooler that way. It's got nothing to do with being able to tell one entity from another.

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

      Yeah i meant to fix this on the site. I caught most of those errors on my main code. Still with all those things fixed its not printing values to the invalid address document, not to mention the regular expression that checks if the source code has an error message is not working properly.

        So are you asking for help on code that differs from what is posted in this thread? Why don't you go ahead and fix all of the errors I mentioned, and then follow up in this thread with new code samples if you're still having errors.

        If this is going to be an iterative process we're going to have to see what the latest version of the code we're debugging looks like.


        Dave

Re: Broken Write function? Does anyone have Magic fixer ability?
by Your Mother (Canon) on Jan 11, 2013 at 03:11 UTC

    *You’re a genius.

Re: Broken Write function? Does anyone have Magic fixer ability?
by frozenwithjoy (Curate) 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 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
      Your bountiful wisdom is appreciated. Yet i still have errors that are not fixed.
      I asked you not to write about formatting and yet, here are formatting tips...

      Also, the comments are spurred by the "continued" inability of anyone to provide constructive aid to any. As far as this community goes. I don't see much value in it...

      PS: Go get more therapy please
      Your bountiful wisdom is appreciated. Yet i still have a problem that is not fixed.
      I asked you not to write about formatting and yet, here are formatting tips...

      Also, the comments are spurred by the "continued" inability of anyone to provide constructive aid to any project. Go get more therapy please, and maybe ill work on becoming "STOIC" like u (this is not rome)

      Strict and warnings are not my problem

      My problem is that the HTML result is not grabbing the source code to verify wether or not the address is valid...
      yes i fixed the other errors, no thanks to you...

      Sometimes i think that I'm the only one here thats posting interesting content.

        The purpose of putting up the entire code is that you can run the code yourself. what a concept.

        U feel silly now huh =(

        then u can use strict and warnings all u want
            Your bountiful wisdom is appreciated. Yet i still have a problem that is not fixed. I asked you not to write about formatting and yet, here are formatting tips...

        And your lack of manners continues.....


        Peter L. Berghold -- Unix Professional
        Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Broken Write function? Does anyone have Magic fixer ability?
by Mr. Muskrat (Abbot) 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 meditating upon the Monastery: (5)
As of 2014-11-29 05:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (203 votes), past polls