http://qs321.pair.com?node_id=249394

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

I need to delete some elements from an XML file, and I'm not sure how to do it intelligently.

I've got an XML file that looks sort of like this:

<opt> <node> <val>1</val> </node> <node> <val>2</val> </node> <node> <val>3</val> </node> </opt>
And I want to remove some arbitrary node based on user input. Here's what I've got so far, assuming that I want to remove the node that has the "val" attribute set to 2:
use strict; use XML::Simple; my $xref = XMLin("<top><node><val>1</val></node><node><val>2</val></no +de><node><val>3</val></node></top>"); my $nodenum = 0; for my $n ( @{$xref->{'node'}} ) { if ( $n->{val} == 2 ) { splice @{$xref->{'node'}}, $nodenum, 1; } $nodenum++; }
I really don't like using the $nodenum++ to track which element I'm iterating at, but I can't think of a better way.