#!/usr/bin/perl use strict; use warnings; use autodie; use XML::LibXML; use Data::Dumper; for my $FName (qw(1041480.1 1041480.2)) { print "----------- SEARCHING DOCUMENT $FName ---------\n"; my $dom = XML::LibXML->load_xml(location=>$FName); my @hits; for my $search ('/root/part/sect/header', '/root/para') { print "----- searching: $search\n"; my $nodeset = $dom->find($search); my $text = join('', map { $_->string_value } $nodeset->get_nodelist); if ($text =~ /design/i) { # found a match! score it and store it my $score = goodness_evaluator($text); push @hits, [ $score, $text ]; } } if (@hits) { @hits = sort {$a->[0] <=> $b->[0]} @hits; my ($score, $text) = @{$hits[0]}; print "$FName: Best match: score=$score, text=$text\n"; } else { print "$FName: no matches found\n"; } } sub goodness_evaluator { my $t = shift; my $score = 0; $score += ord($_) for $t=~m/(.)/g; return $score; }