in reply to replacing continuesly occuring pattern

My favourite XML method ... XML::Twig! ;-)

#!/usr/bin/perl -w use strict; use XML::Twig; my $xml = XML::Twig->new( pretty_print => 'indented', ); $xml->parse(\*DATA); # get all parents. my @parents = do { my %name = map { my $p = $_->parent(); "$p" => $p; } $xml->root()->get_xpath('//name'); values %name; }; foreach my $parent (@parents) { my @name = $parent->get_xpath('name'); my @names = map { $_->cut(); $_->text() } @name; my $names = XML::Twig::Elt->new('names', join ',', @names); $names->paste($parent); } $xml->print; __END__ <doc> <set> <name>abc</name> <name>def</name> <name>ghi blah</name> </set> <set> <name>jkl</name> <name>mno</name> <name>pqr blah</name> </set> <set> <name>abc</name> <name>def</name> <name>ghi blah</name> </set> </doc>
Output is:
<doc> <set> <names>abc,def,ghi blah</names> </set> <set> <names>jkl,mno,pqr blah</names> </set> <set> <names>abc,def,ghi blah</names> </set> </doc>
There is probably an easier way to do this with XML::Twig using the twig handlers, but those confuse the heck out of me, so I try to avoid them ;-) I find this method, even if a bit longer, and probably slower and more memory-consuming, to be easier to understand. To me, it has a much higher degree of WUD (thanks, friedo, for that term!) :-)