Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Error with XML::XPath::Literal

by perl_seeker (Scribe)
on Nov 03, 2004 at 11:30 UTC ( #404864=perlquestion: print w/ replies, xml ) Need Help??
perl_seeker has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
I am using XML::XPath to parse an xml file which I've put at the bottom of the post.

In the following code while trying to find the sum of all the elements in @totrain and print it,I keep getting the
following error. Any suggestions on how to remove this?

Code
sub RainSoFarMessage{ my $ulimit = shift; #print "\n$ulimit"; my $xp = XML::XPath->new(filename => 'weather_records.xml'); my @totrain=(); my $nodes; for ($month=1;$month<=$ulimit;$month++){ #print "\n$month"; my $pattern = "$month-"; #print "\n$pattern"; $nodes = $xp->findnodes("//MonthlyWeatherRecord [contains(date,'$pattern')]"); foreach my $node($nodes->get_nodelist){ push @totrain,$xp->findvalue(".//totalrainfall/ \@number",$node); } } #print "\n@totrain"; my $total=0; #print "\n$total"; foreach my $t(@totrain){ #print "\n$t"; $total=$total + $t; #print "\n$total"; } return $total; }
Main
my $monthrs = 2; my $totalrs = RainSoFarMessage($monthrs); #print "\n$totalrs"; my @RainSoFarMesg=(); push @RainSoFarMesg,$monthrs,$totalrs; #print "\n@RainSoFarMesg";
Error
Operation `+': no method found, left argument has no overloaded magic, right argument in overloaded package XML::XPath::Literal at C:\..\ +..\xmlrecord_parse.pl line 480. Tool completed with exit code 255
Line 480 is this:

$total=$total + $t;

Any help would be appreciated.
Thanks,
perl_seeker :)
weather_records.xml - <AnnualWeatherRecord> - <DailyWeatherRecord> <date>1-1-2004</date> - <temperature> <maxdrybulb unit="degrees-centigrade" number="33.95" /> <mindrybulb unit="degrees-centigrade" number="30.95" /> <maxwetbulb unit="degrees-centigrade" number="33.53" /> <minwetbulb unit="degrees-centigrade" number="30.53" /> </temperature> <totalrainfall unit="mm" number="0.5" /> </DailyWeatherRecord> - <DailyWeatherRecord> <date>2-1-2004</date> - <temperature> <maxdrybulb unit="degrees-centigrade" number="23.34" /> <mindrybulb unit="degrees-centigrade" number="23.20" /> <maxwetbulb unit="degrees-centigrade" number="23.14" /> <minwetbulb unit="degrees-centigrade" number="20.14" /> </temperature> <totalrainfall unit="mm" number="0" /> </DailyWeatherRecord> - <DailyWeatherRecord> <date>1-2-2004</date> - <temperature> <maxdrybulb unit="degrees-centigrade" number="44.25" /> <mindrybulb unit="degrees-centigrade" number="39.25" /> <maxwetbulb unit="degrees-centigrade" number="33.53" /> <minwetbulb unit="degrees-centigrade" number="30.53" /> </temperature> <totalrainfall unit="mm" number="0.5" /> </DailyWeatherRecord> - <DailyWeatherRecord> <date>2-2-2004</date> - <temperature> <maxdrybulb unit="degrees-centigrade" number="50.25" /> <mindrybulb unit="degrees-centigrade" number="49.25" /> <maxwetbulb unit="degrees-centigrade" number="23.14" /> <minwetbulb unit="degrees-centigrade" number="20.14" /> </temperature> <totalrainfall unit="mm" number="0" /> </DailyWeatherRecord> - <MonthlyWeatherRecord> <date>1-2004</date> - <temperature> <maxdrybulb unit="degrees-centigrade" number="33.95" /> <mindrybulb unit="degrees-centigrade" number="23.20" /> <avgdrybulb unit="degrees-centigrade" number="33.95" /> </temperature> <totalrainfall unit="mm" number="0.5" /> </MonthlyWeatherRecord> - <MonthlyWeatherRecord> <date>2-2004</date> - <temperature> <maxdrybulb unit="degrees-centigrade" number="50.25" /> <mindrybulb unit="degrees-centigrade" number="39.25" /> <avgdrybulb unit="degrees-centigrade" number="44.25" /> </temperature> <totalrainfall unit="mm" number="0.5" /> </MonthlyWeatherRecord> </AnnualWeatherRecord>

Comment on Error with XML::XPath::Literal
Select or Download Code
Re: Error with XML::XPath::Literal
by rob_au (Abbot) on Nov 03, 2004 at 11:54 UTC

    You might want to try this (untested) solution - Change line 480 to read:

    $total += sprintf( "%d", $t );

    The problem results because of the overloaded nature of $t.

     

    perl -le "print unpack'N', pack'B32', '00000000000000000000001011101101'"

      Hello Rob!
      Your solution works, thanks a lot! I have used "%f" instead of "%d", since I need to add floating point numbers.
      Cheers!
      perl_seeker:)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2014-08-01 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (20 votes), past polls