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

Hi I have two files ,the first data.xml which contain the following:
<?xml version="1.0" ?> <output> <message><file>me.pl</file><line>23</line><type>note</type><codee>345< +/codee><extra>sente</extra> <message><file>you.pl</file><line>25</line><type>warning</type><codee> +355</codee><extra>sente</extra> <message><file>hi.pl</file><line>21</line><type>note</type><codee>358< +/codee><extra>sente</extra> <message><file>she.pl</file><line>123</line><type>warning</type><codee +>3583</codee><extra>sente</extra> <message><file>they.pl</file><line>231</line><type>warning</type><code +e>3582</codee><extra>sente</extra> <message><file>them.pl</file><line>26</line><type>note</type><codee>33 +58</codee><extra>sente</extra> </output>
where the fist number appear in each line is a line number in that code now in my second file which is Changes.txt , I have the following
you.pl 24-28 they.pl 36 them.pl 44-49 you.pl 234 77
which are lines that changed in a code, 24-28 means 24 to 28 ... so what I need to do is reading from the Changes.txt file each line and see if the .pl file appears in the first file data.txt , if it does then I want to see if the lines number matches or in the same range and it is a warning , if it match and it is a warning then I want to get the hole line and send it to a file . example you.pl appears in both files the lines for you.pl in the Changes.txt are 24 to 28 and in data.txt is 25 , line 25 which is in the range of 24-28 appear and it is a warning so that is what I am looking for so I copy the hole line
<message><file>you.pl</file><line>25</line><type>warning</type><codee> +355</codee><extra>sente</extra>
to an output file .... Now, I am trying to do this somehow, but xml is not an easy to parse ..
open(CHANGES, "< $linesChangedList") or die "could not open file"; my $myFile = $myFile0; while (<CHANGES>) { chomp; $info = $_; # is it a range or a single line? if ($info =~ /(\d+)-(\d+)/) { $changes{$myFile0} .= " ".join(" ",($1 .. $2))." "; } else { $changes{$myFile0} .= " $info "; } } close(CHANGES); while (<DATA>) { chomp; # split the line from the wave characters my ($file, $line, $type, $rest) = split(/~/, $_, 4); # was this file found in the previous loop? if (defined $changes{$file}) { # was it a range? if (defined $changes{$file}->[1]) { # skip if this $line is not in this range next unless ($line gt $changes{$file}->[0] and $line lt $changes +{$file}->[1]); } else { # skip if this $line is not equal to this single line number next unless $line eq $changes{$file}->[0]; } # if we are here, then this is a match. so print it. print "$_\n"; } }
can someone tell me how to do that or give me a hint :) thanks

Replies are listed 'Best First'.
Re: xml parsing
by Abstraction (Friar) on Jul 18, 2002 at 14:27 UTC
    In regards to parsing XML, I would recommend using one of the many XML modules available on CPAN. There is no need to write your own XML parser when several have allready been written and tested.

    That being said, I would recommend XML::Simple for this task.
Re: xml parsing
by mirod (Canon) on Jul 18, 2002 at 16:34 UTC

    First, as mentionned previously, do not write your own XML parser, you won't get it right.

    As you XML seems to be organised in record-like structures, you might want to have a look at AnyData, which will let you treat the XML as a relational table.

    --
    The Error Message is GOD - MJD