use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new(); $twig->parse (do {local $/; }); my @chunks = $twig->root ()->children(); my %lookup; for my $chunk (@chunks) { my $words = $chunk->first_child ('words'); my $text = $words->xml_text (); push @{$lookup{$_}}, $chunk for split /,\s+/, $text; } #do stuff with %lookup __DATA__ c:\data\cat.xml dog, cat, fish, bird c:\data\cow.xml dog, cat, fish, bird, cow, goat c:\data\snake.xml dog, cat, fish, bird, snake, orange