For hashes you want the key to be something that doesn't change, in fact you don't just want it to be, it has to be. So if we reverse your hash so that the sentence itself is the key your code would be:

So you say...but i want my hash that way. And to that I say "no you don't". :) If you think about it, you will have problems if you use the keys. First of all there is no easy way to change the key of a value. You would have to set the new key to the value and then remove the old one. This would be fine except you can only have one value stored per key. Which means when your first sentence counts 4 occurences of something its key is now 39 just like your second sentence. At that point one of those sentences is oblitirated. This is easily solved by storing the value that you want to change as, you guessed it, the value.

The code below does what you want, i think. First it builds a hash with your sentences and there current counts. Then it builds an array of words to match. I take the array of words you want to match agianst and join them together with | to make a regex that will match any one of the words (thats what the | means). Then i compile that together into a regex with \b's to say that you only want to match those on word boundaries (i.e. your word has to be the whole word and nothing more). From there we just loop over the keys in the hash, matching those keys against the regex and incrementing there value for every match found. I hope this helps you solve your problem.

use strict; use warnings; use Data::Dumper; my %UnNum = ( "forc two bodi twice strong on bodi sai bodi mass" => 35, "you expect on think new bodi made two bodi origin" => 39, "attract bodi b origin force" => 60, "total forc b twice origin force" => 46, "sai on bodi twice mass three time mass forc six" => 34, "on now see why bodi fall same rate bodi twice" => 17 ); my @midWords = qw/bodi forc twice mass acceler law/; my $joined = join("|", @midWords); my $regex = qr/\b$joined\b/; print Dumper(\%UnNum); for my $sentence (keys %UnNum) { $UnNum{$sentence}++ while $sentence =~ /$regex/g; } print Dumper(\%UnNum);

___________
Eric Hodges

In reply to Re: Counting instances in a hash from an array by eric256
in thread Counting instances 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.