Two suggestions:

1) if the data is machine generated and you know that it will be identical when the gene sequence etc. is identical, that is to say you know that you will never see the tags presented in a a different order then you can usefully compare whole records at a time by setting the input record seperator appropriately:

$/ = '</species>.$/; my $prev_rec=''; while (<INFILE>) { next if ($_ eq $prev_rec); $prev_rec = $_; # process $_ somehow as it is unique }
Note that in addition to the drawback noted above, this code requires records that are identical to be adjacent.

2) A better way is probably to XML::Twig (or XML::Simple perhaps) the file and compare the resulting data structures. This avoids the tags must be in identical order problem and, depending on your code, may also avoid the identical records must be adjacent problem too.

A cunning way to compare two arbitrary data structures is to use Data::Dumper and string comparisons:

use Data::Dumper; if (Dumper(\%struct1) eq Dumper(\%struct2)) { do something; }
The disadvantage is that this second method will be quite a lot slower. Because I quite like processing enormous files quickly I have used methods similar to the first method successfully on records pulled from pubmed.

Dingus


Enter any 47-digit prime number to continue.

In reply to Re: Removing duplicate subtrees from XML by dingus
in thread Removing duplicate subtrees from XML by matth

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.