Hello Johnny!
I still cannot do much with the module. Maybe I'm asking for too much, but could you please help me with this?(thanks for your patience!):
I need to write a subroutine that takes as input the month number e.g. 2 for February, and searches the XML file for the monthly weather record for February, and any month before it i.e. January.I need to extract the total rainfall amounts from these records and find their sum.
The subroutine returns the month number and the sum in an array.If I needed to look for only the February monthly record or $month='2', I guess the code would look like this:
sub RainSoFarMessage{
my $month = shift;
$pattern = "$month-";
my $xp = XML::XPath->new(filename
=> 'weather_records.xml');
my @result = ();
my @totrain=();
my $nodes = $xp->findnodes("//MonthlyWeatherRecord
[contains(date,'$pattern')]");
foreach my $node($nodes->get_nodelist){
push @totrain,$xp->findvalue(".//totalrainfall/
\@number",$node);
}
my $total=0;
foreach my $t(@totrain){
$total=$total + $t;
print "\n$total";
}
push @result [$month,$total];
return \@result;
}
I know this seems silly, but how can I set the value of '$pattern' so that the search pattern includes the month passed as a parameter to the sub, as well as all months before it, starting from Jan? Please help!
I also need to write another sub, which also takes the month number as input, and searches the daily weather records for that month and extracts the maxdrybulb values . I need to search for each set of 5 (for the time being say, better to make this a variable that can be set) consecutive days where the maxdrybulb values fall in the same range( 'temperature spells' of the month).
The temperature ranges could be defined as for example:
Over 40.0 extremelyhot
35.0 to 39.9 veryhot
30.0 to 34.9 hot
25.0 to 29.9 verywarm
I need to then build an array of arrays, where each array corresponds to a temperature spell (5 consecutive days with max. temp in the same range) and stores the start date, end date, and the string representing the temp. range('hot','verywarm', etc.). For e.g.,
1-2-2004
5-2-2004
'verywarm'
The sub returns this array of arrays to main. Of course right now the xml file above has only 2 daily weather records for each month.My monthly weather records now look like this, with rainfall info added.
<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>
All this seems really complicated. I do have some code, but not much. Please help!
Thanks,
perl_seeker:)
| [reply] [d/l] [select] |
Hello,
thanks a lot, this really helps. Could you suggest
a good tutorial for Xpath or for the perl module XML::XPath,
so that I can start doing some things?
Cheers!
perl_seeker :)
| [reply] |
http://www.w3schools.com/ has some nice tutorials on XML in general. There may be an XPath tutorial by now. In fact there is.
--Solo
--
You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.
| [reply] |