Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • 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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-28 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found