in reply to Matching combination of words in a tag
One comment on your attempt -- it looks like you were trying to re-read <DATA> inside in the inner loop .. you would have to seek to the beginning tfor that to work, but should just store it in a variable before the loops -- other wise it's a waste of expensive I/O to keep opening the file up.use strict; use warnings; my $s = do {local $/=undef; <DATA>}; use XML::Simple; my $data = XMLin("<opt>$s</opt>", KeepRoot => 0); #use Data::Dumper; #print Dumper $data; my $search_string = "dog cat cow"; # from input my %searchwords = map { $_ => undef } split ' ', $search_string; while( my ($k, $h) = each %$data ){ my $score = scalar grep( exists $searchwords{$_}, split(/, /, $h->{w +ords}) ); # do whatever based on score. } __DATA__ <MS_1> <loc>c:\data\cat.xml</loc> <words>dog, cat, fish, bird</words> </MS_1> <MS_2> <loc>c:\data\cow.xml</loc> <words>dog, cat, fish, bird, cow, goat</words> </MS_2> <MS_3> <loc>c:\data\snake.xml</loc> <words>dog, cat, fish, bird, snake, orange</words> </MS_3>
|
|---|