This is buggy as far as scoring, but the idea is here.

#!/usr/bin/perl -w use strict; my $text = qq|If you have a question on how to do something in Perl, o +r you need a Perl solution to an actual real-life problem, or you're unsure why something you've tri +ed just isn't working... then this is the place to ask. However, you might consider asking in the chatterbox first (if you're +a registered user). The response time tends to be quicker, and if it turns out that the pr +oblem/solutions are too much for the cb to handle, the kind monks will be sure to direct y +ou here.|; # build lines hash my %lines; my $k=1; for (split(/\n/,$text)){ $lines{$k}=$_; $k++; } # score hash # put your words here, and the value for each my %score_guide = ( 'for'=>1, 'the'=>1, 'if'=>2, 'you'=>3, 'moguai'=>40 ); # this will hold key= line num, val= score my %score_results =(); # score each line for (keys %lines){ $score_results{$_} = score_line($lines{$_}); } # feedback for (keys %score_results){ print "line $_ score $score_results{$_}\n"; } sub score_line { my ($string)=$_[0]; # my $score=0; for (sort keys %score_guide){ while ($string=~s/\b\Q$_\E\b//i){ # corrected by ikegami $score+=$score_guide{$_}; } } return $score; }

output:

[leo@mescaline ~]$ perl scorelines.pl line 1 score 8 line 2 score 6 line 3 score 1 line 4 score 9 line 5 score 4 line 6 score 6

In reply to Re: Increasing key/values held in a Hash from an Array by leocharre
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.