Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2022-12-08 15:46 GMT
Find Nodes?
    Voting Booth?

    No recent polls found