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>