Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I am working on this weather script and I am fetching 15 zip codes as an example of my issue. The issue is that every time I run the code I have random missing values, am I doing something wrong on the Perl side, is there a better way of doing this? Here is a code sample and you can see these random missing values on the "$city" variable if you ran this.
#!/usr/bin/perl -w use strict; use CGI qw(-oldstyle_urls :standard); use XML::TreePP; use XML::XPath; use XML::Simple; use Data::Dumper; use Date::Format; use Date::Parse; my $q = new CGI; $| = 1; my @zipcode = qw(02151 01908 02638 02669 02670 02536 02642 02574 02332 + 02601 02045 01930 02152 27943 02169); my ($currtempf, $currtempc, $currhumidity, $currcondition, $currwind, +$todayhigh, $todaylow, $todaycond, $tomorrowcond); my ($city, $url, $tpp, $tree); foreach my $zip(@zipcode) { next unless $zip =~ /^(\d{5})$/; $url = "http://www.google.com/ig/api?weather=".$zip ; $tpp = XML::TreePP->new(); $tree = $tpp->parsehttp( GET => $url ); $city = $tree->{xml_api_reply}->{weather}->{forecast_information}-> +{city}->{"-data"} || ''; $currtempf =$tree->{xml_api_reply}->{weather}->{current_conditions} +->{temp_f}->{"-data"} || ''; $currtempc =$tree->{xml_api_reply}->{weather}->{current_conditions} +->{temp_c}->{"-data"} || ''; $currhumidity =$tree->{xml_api_reply}->{weather}->{current_conditio +ns}->{humidity}->{"-data"} || ''; $currcondition =$tree->{xml_api_reply}->{weather}->{current_conditi +ons}->{condition}->{"-data"} || ''; $currwind =$tree->{xml_api_reply}->{weather}->{current_conditions}- +>{wind_condition}->{"-data"} || ''; $todayhigh =$tree->{xml_api_reply}->{weather}->{forecast_conditions +}->[0]->{high}->{"-data"} || ''; $todaylow =$tree->{xml_api_reply}->{weather}->{forecast_conditions} +->[0]->{low}->{"-data"} || ''; $todaycond =$tree->{xml_api_reply}->{weather}->{forecast_conditions +}->[0]->{condition}->{"-data"} || ''; $tomorrowcond =$tree->{xml_api_reply}->{weather}->{forecast_conditi +ons}->[1]->{condition}->{"-data"} || ''; print "**City=$city** - Url=**$url**\n"; }

Thanks for looking and the help!!!

Replies are listed 'Best First'.
Re: Missing values when running code.
by Kenosis (Priest) on Aug 14, 2012 at 14:25 UTC

    I suspect your requests were intermittently dropped, as such would account for the "random missing values."

      This is what I am taking about, is this related to the google API? Is there anything that can be done to avoid such "intermittently" drop on the requests?

        Yes, sorry: my first response wasn't too helpful. Replacing $tree = $tpp->parsehttp( GET => $url ); with the following consistently returned all cities:

        my $count = 5; $tree = $tpp->parsehttp( GET => $url ) while !defined $tree and $count +--;

        Using $count helps to insure the script's not forever caught in the while loop.

        Update: Modified the description and tweaked the $count routine.