Inserting a pause into a loop
Hi,
I'm writing a perl script thats goal is to read a delimited file =
containing a userid and an address to google maps where the address is =
converted into latitude and longitude. The problem I'm having is with =
the result I'm printing. Each line is unique via the userid when its =
printed but I'm getting the same latitude and longitude for each =
address. I think the problem is I need to wait x amount of seconds =
before i send each url to google, but I'm not really sure how to =
accomplish that. To be truthful I'm not even sure that is the problem. =
I've attached my script, input file, and result. Note its personal data =
so I've had to anonymize the input file and the results.
=
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++=
++
#!/usr/bin/perl
use warnings;
use strict;
use LWP::Simple;
open (ADDRESSOUT, 'addressout.txt');
while (<ADDRESSOUT>) { #the start of the loop
chomp; #dropping line characters
my [at] addressfield =3D split(/\:\:\:/, $_);
my $geoaddress =3D $addressfield[1] =3D~ s/ /\+/;
my $googlekey =3D "unique google map api key";
my $geocode_csv =3D =
get("http://maps.google.com/maps/geo?q=3D$geoaddress&output= 3Dcsv&sensor=3D=
false&key=3D$googlekey")
or die 'Unable to get page';
my [at] geoarray =3D split(/,/, $geocode_csv);
print "$addressfield[0],$geoarray[2],$geoarray[3]\n";
} #end of the loop
close (ADDRESSOUT); #closing the file
exit 0;
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
addressout.txt
32986000382444:::34 main street anytown, tn 39455
31686000146174:::76 second avenue anytown, tn 39455
31685000781354:::1 park lane anytown, tn 39455
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
result
32986000382444 ,49.3961500,32.7630300
31686000146174 ,49.3961500,32.7630300
31685000781354 ,49.3961500,32.7630300
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Inserting a pause into a loop
Erik Lewis wrote:
> Hi,
Hello,
> I'm writing a perl script thats goal is to read a delimited file
> containing a userid and an address to google maps where the address is
> converted into latitude and longitude. The problem I'm having is with
> the result I'm printing. Each line is unique via the userid when its
> printed but I'm getting the same latitude and longitude for each
> address. I think the problem is I need to wait x amount of seconds
> before i send each url to google, but I'm not really sure how to
> accomplish that. To be truthful I'm not even sure that is the problem.
> I've attached my script, input file, and result. Note its personal
> data so I've had to anonymize the input file and the results.
>
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++
> #!/usr/bin/perl
> use warnings;
> use strict;
> use LWP::Simple;
>
> open (ADDRESSOUT, 'addressout.txt');
You should *always* verify that the file opened correctly:
open ADDRESSOUT, '<', 'addressout.txt' or die "Cannot open
'addressout.txt' $!";
> while (<ADDRESSOUT>) { #the start of the loop
> chomp; #dropping line characters
> my [at] addressfield = split(/\:\:\:/, $_);
>
> my $geoaddress = $addressfield[1] =~ s/ /\+/;
You are assigning the return value of the substitution which is always
either TRUE or FALSE (the =~ operator has higher precedence than the =
operator.) You need to do the assignment first before the substitution.
You are only substituting the first space character, not all of them.
( my $geoaddress = $addressfield[ 1 ] ) =~ s/ /+/g;
> my $googlekey = "unique google map api key";
> my $geocode_csv = get("http://maps.google.com/maps/geo?q=$geoaddress&output=cs v&sensor=false&key=$googlekey")
> or die 'Unable to get page';
> my [at] geoarray = split(/,/, $geocode_csv);
> print "$addressfield[0],$geoarray[2],$geoarray[3]\n";
> } #end of the loop
>
> close (ADDRESSOUT); #closing the file
> exit 0;
>
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> addressout.txt
> 32986000382444:::34 main street anytown, tn 39455
> 31686000146174:::76 second avenue anytown, tn 39455
> 31685000781354:::1 park lane anytown, tn 39455
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> result
> 32986000382444 ,49.3961500,32.7630300
> 31686000146174 ,49.3961500,32.7630300
> 31685000781354 ,49.3961500,32.7630300
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Inserting a pause into a loop
Wow, thank you for your assistance. I was working on the assumption =
that I got something correct in an earlier script, that was incorrect. =
I've got some more work to do but your explanation helped me understand =
where I went wrong.
On Feb 24, 2010, at 12:38 PM, John W. Krahn wrote:
> Erik Lewis wrote:
>> Hi,
>
> Hello,
>
>> I'm writing a perl script thats goal is to read a delimited file
>> containing a userid and an address to google maps where the address =
is
>> converted into latitude and longitude. The problem I'm having is =
with
>> the result I'm printing. Each line is unique via the userid when its
>> printed but I'm getting the same latitude and longitude for each
>> address. I think the problem is I need to wait x amount of seconds
>> before i send each url to google, but I'm not really sure how to
>> accomplish that. To be truthful I'm not even sure that is the =
problem.
>> I've attached my script, input file, and result. Note its personal
>> data so I've had to anonymize the input file and the results. =
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++=
++
>> #!/usr/bin/perl
>> use warnings; use strict; use LWP::Simple;
>> open (ADDRESSOUT, 'addressout.txt');
>
> You should *always* verify that the file opened correctly:
>
> open ADDRESSOUT, '<', 'addressout.txt' or die "Cannot open =
'addressout.txt' $!";
>
>
>> while (<ADDRESSOUT>) { #the start of the loop
>> chomp; #dropping line characters
>> my [at] addressfield =3D split(/\:\:\:/, $_); my $geoaddress =3D =
$addressfield[1] =3D~ s/ /\+/;
>
> You are assigning the return value of the substitution which is always =
either TRUE or FALSE (the =3D~ operator has higher precedence than the =3D=
operator.) You need to do the assignment first before the =
substitution. You are only substituting the first space character, not =
all of them.
>
> ( my $geoaddress =3D $addressfield[ 1 ] ) =3D~ s/ /+/g;
>
>
>> my $googlekey =3D "unique google map api key";
>> my $geocode_csv =3D =
get("http://maps.google.com/maps/geo?q=3D$geoaddress&output= 3Dcsv&sensor=3D=
false&key=3D$googlekey")
>> or die 'Unable to get page';
>> my [at] geoarray =3D split(/,/, $geocode_csv);
>> print "$addressfield[0],$geoarray[2],$geoarray[3]\n";
>> } #end of the loop
>> close (ADDRESSOUT); #closing the file
>> exit 0;
>> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>> addressout.txt
>> 32986000382444:::34 main street anytown, tn 39455
>> 31686000146174:::76 second avenue anytown, tn 39455
>> 31685000781354:::1 park lane anytown, tn 39455
>> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>> result
>> 32986000382444 ,49.3961500,32.7630300
>> 31686000146174 ,49.3961500,32.7630300
>> 31685000781354 ,49.3961500,32.7630300
>> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
>
>
> John
> --
> The programmer is fighting against the two most
> destructive forces in the universe: entropy and
> human stupidity. -- Damian Conway
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/