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);
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |