Your data structure is malformed. You don't want the key to be the score, that is, something subject to change.

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.

So first, we take your hash and replace it with a saner data structure:

my @scorearray = map { {score=>$_, text=>$hash{$_}} } keys %hash;

Now that that's done, what you ask is quite easy:

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; } }

You can even print it out in a pretty table:

printf "%-7s%s\n", "Score", "Value"; for my $scorebit (@scorearray) { printf "%5d %s\n", $scorebit->{'score'}, $scorebit->{'text'}; }
--
@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

In reply to Re: Increasing key/values held in a Hash from an Array by fizbin
in thread Increasing key/values held in a Hash from an Array by Gavin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.