use strict; use warnings; use Smart::Comments '###'; my $abstract = 'Perl is a high-level, general-purpose, interpreted, dynamic programming language.'; my $MinLength = 2;#Must be at least 1; my $MaxLength = 4;#Must be >= $MinLength my $MinPosition = $MinLength - 1;#Counting from 0 my ( $ArrayRef, $AnswerRef ) = (); @$ArrayRef = split(/\s+/, $abstract); #Iterate through @$ArrayRef for my $position (0..($#$ArrayRef-$MinPosition)) { #handle the first word with no space my $phrase = $ArrayRef->[$position]; for my $x (1..$MaxLength) {#Build the phrase #For all but the first time $phrase .= ' ' . $ArrayRef->[$position] if $x != 1; #Load the output $AnswerRef->[$x]->{$phrase} = 1 if $x >= $MinLength; $position++; #Bump up against the end last if $position > $#$ArrayRef; } } #use Smart::Comment.pm ### The unique 3 word phrases are: $AnswerRef->[3]