#!/usr/bin/perl/ use warnings; use strict; my $phrases = []; # Array of Hashes containing the different # length phrases...the index of each entry # is the number of words in tne phrases, the # hash contains all of the phrases of that length, # exactly as the OP did in the example my $maxPhraseLen = 4; # to match the OPs example. Can be set to any # value from 1 to the Length of the Abstract my $abstract = 'Perl is a high-level, general-purpose, interpreted, '; $abstract .= 'dynamic programming language.'; my @words = split(/\s+/, $abstract); die "Max Phrase Length exceeds number of words in abstract: aborting\n" if($maxPhraseLen > (scalar @words)); foreach my $numWordsInPhrase (1..$maxPhraseLen){ foreach my $index (0..($#words-$numWordsInPhrase)) { my $phrase = ""; # clear out the phrase accumulator for(my $i = 0; $i<$numWordsInPhrase; $i++){ $phrase .= $words[($index+$i)] . ' '; } $phrases->[$numWordsInPhrase]->{$phrase} = undef; } } print "\n"; foreach(my $i = 1; $i<(scalar @$phrases); $i++){ print "PHRASE LENGTH $i:\n"; foreach my $phrase (keys %{$phrases->[$i]}){ print " $phrase\n"; } } exit(0);