while (<>) { push @words, split /\s/; } #### trigram frequencies in your text: iwentthere! 1 wentthere!she 1 there!shealso 1 shealsowent 1 alsowentthere. 1 #### @trigrams = (); while (<>) { @words = split /\s/, $_; for ( $i = 0 ; $i < $#words - 1 ; $i++ ) { $trigram = $words[$i] . $words[ $i + 1 ] . $words[ $i + 2 ]; $found = -1; if (@trigrams) { SEARCHtrigramINDEX: for ( $index = 0 ; $index <= $#trigrams ; $index++ ) { if ( $trigrams[$index] eq $trigram ) { $found = $index; last SEARCHtrigramINDEX; } } } if ( $found > -1 ) { $trigramfrequency[$found]++; } else { push @trigrams, $trigram; $trigramfrequency[$#trigrams]++; } } } print "trigram frequencies in your text:\n"; for ( $index = 0 ; $index <= @trigrams ; $index++ ) { print "$trigrams[$index] $trigramfrequency[$index]\n"; }