Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
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 making s'mores by the fire in the courtyard of the Monastery: (14)
As of 2015-07-02 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (31 votes), past polls