http://www.perlmonks.org?node_id=1009716


in reply to count trigrams of a whole file

A couple of improvements you can make:
#!usr/bin/perl use strict; use warnings; my %trigrams; my @words; while(<DATA>) { #Include the previous two words to the beginning of this array. @words = ( $words[-2] // (), $words[-1] // (), split(/\s/, $_) ); $trigrams{"@words[$_..$_+2]"}++ for (0..$#words-2); } print "trigram frequencies in your text:\n"; #Sort the trigrams in descending order of frequency. for (sort {$trigrams{$b} <=> $trigrams{$a} } keys %trigrams) { print "$_: $trigrams{$_}\n"; } __DATA__ I went there! Me She also went there. Did you know that I went there!

Replies are listed 'Best First'.
Re^2: count trigrams of a whole file
by lakssreedhar (Acolyte) on Dec 21, 2012 at 18:28 UTC

    i need the words and its count printed in the order of words given in the file

      Thanks i got it