I'm assuming you want to count how many of each word pair is in the document, and rank the words by number found. I'm also assuming that a line break is always the end of a sentence, and that you want periods to signify the end of a sentence.
use strict; my (@lines, @words, $i, $pair, %hash); foreach (<DATA>) { $_ =~ s/[^\w\. ]//g; # Remove unneeded characters $_ =~ s/ +/ /g; # Many spaces to one $_ =~ s/ ?\. ?(\. ?)*/\./g; # Boundaries with . change to . $_ =~ s/^ //; $_ =~ s/ $//; # Spaces at start and end removed $_ = lc($_); # Lowercase @lines = split(/\./, $_); # Split on sentence boundaries foreach (@lines) { # Get words for each sentence @words = split(/[\. ]/, $_); for ($i = 0; $i < $#words; $i++) { # For each word pair $pair = @words[$i] . ' ' . @words[$i+1]; $hash{$pair}++; # Increment count for word pair } } } foreach (sort {$hash{$b} <=> $hash{$a}} keys %hash) { print $_ . ' ' . $hash{$_} . "\n"; } __DATA__ Four score and seven years ago our fathers brought forth, upon this co +ntinent, a new nation, conceived in liberty, and dedicated to the pro +position that "all men are created equal" Now we are engaged in a great civil war, testing whether that nation, +or any nation so conceived, and so dedicated, can long endure. We are + met on a great battle field of that war. We have come to dedicate a +portion of it, as a final resting place for those who died here, that + the nation might live. This we may, in all propriety do. But, in a l +arger sense, we can not dedicate -- we can not consecrate -- we can n +ot hallow, this ground -- The brave men, living and dead, who struggl +ed here, have hallowed it, far above our poor power to add or detract +. The world will little note, nor long remember what we say here; whi +le it can never forget what they did here. It is rather for us, the living, we here be dedicated to the great tas +k remaining before us -- that, from these honored dead we take increa +sed devotion to that cause for which they here, gave the last full me +asure of devotion -- that we here highly resolve these dead shall not + have died in vain; that the nation, shall have a new birth of freedo +m, and that government of the people by the people for the people, sh +all not perish from the earth.

In reply to Re: Word Pairs and Lines by TedPride
in thread Word Pairs and Lines by bob

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.