note
fizbin
<p>Your data structure is malformed. You don't want the key to be the score, that is, something subject to change.</p>
<p>What happens if I bump up the score of something that was at 13 by two, but there's another sentence at 15? Or what if there's a sentence at 12, and one at 13, and the 12 one gets bumped up by 3 and the 13 one gets bumped up by two? What's going to happen with that data structure is that one or the other will get wiped out.</p>
<p>So first, we take your hash and replace it with a saner data structure:</p>
<code>
my @scorearray = map { {score=>$_, text=>$hash{$_}} } keys %hash;
</code>
<p>Now that that's done, what you ask is quite easy:</p>
<code>
for my $word (@scoreWords) {
my $qmword = quotemeta($word);
my $regexp = qr/\b$qmword\b/;
for my $scorebit (@scorearray) {
$scorebit->{'score'} += 1 if $scorebit->{'text'} =~ $regexp;
}
}
</code>
<p>You can even print it out in a pretty table:</p>
<code>
printf "%-7s%s\n", "Score", "Value";
for my $scorebit (@scorearray) {
printf "%5d %s\n", $scorebit->{'score'}, $scorebit->{'text'};
}
</code>
<div class="pmsig"><div class="pmsig-246930">
--<br>
<code>@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/;
map{y/X_/\n /;print}map{pop@$_}@/for@/</code>
</div></div>
537579
537579