The only punctuation marks you really have to worry about are - and '. If - occurs at the end of the line, the word closest to the end of the line has been split between that line and the next line, and has to be wrapped over. If ' occurs inside a word, it's part of the word and has to be preserved; otherwise, it is removed. All other punctuation marks are removed. A sloppy version of this is as follows:

EDIT: I wasn't accounting for extra spaces on the start, end or middle of each line. Updated code and added comments.

EDIT: Also updated so comments fit properly.

use strict; use warnings; my (%count, $last); my $max = 0; while (<DATA>) { s/^ +//; s/\s+$//; s/ +/ /g; ## Remove extra spaces $_ = lc($_); ## Lowercase so not sensitive $_ = $last . $_; ## Append word piece if (m/-$/) { ## If line ends in -, remove last ($_, $last) = m/(.*?) ?(\w+)-/; ## word piece for appending } else { $last = ''; } ## Else word piece is nothing s/[^\w' ]//g; ## Remove extra punctuation s/(\w)'(\w)/$1-$2/g; ## Convert ' inside words to - s/'//g; ## Remove all remaining ' s/-/'/g; ## Convert - back to ' for (split / +/) { ## Split on space and $count{$_}++; ## process words $max = length() if length() > $max; ## Find longest word size } } print sprintf('%'.$max.'s', $_) . " => $count{$_}\n" for sort { $count{$b} <=> $count{$a} || $a cmp $b} keys %count; __DATA__ I can't be bought, and I won't be bought! My school- house is my own, my precious. One school to rule them all! This is my cant, my creed. Funky space check!

In reply to Re: Frequency of words in text file and hashes by TedPride
in thread Frequency of words in text file and hashes by perl_seeker

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.