Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Removing duplicate subtrees from XML

by dingus (Friar)
on Dec 03, 2002 at 08:28 UTC ( [id://217155]=note: print w/replies, xml ) Need Help??


in reply to Removing duplicate subtrees from XML

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://217155]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-23 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found