An array slice might help here
@array[$start..$end]
#! /usr/bin/perl use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent = 2; my $abstract = q{ Perl is a high-level, general-purpose, interpreted, dynamic programming language. }; my @words = split(q{ }, $abstract); my %phrase_table; my $count = $#words; for my $phrase_length (2..4){ for my $i (0..$count-$phrase_length+1){ push @{$phrase_table{$phrase_length}}, [ @words[$i..$i+$phrase_length-1] ]; } } print Dumper \%phrase_table;
output
$VAR1 = { '4' => [ [ 'Perl', 'is', 'a', 'high-level,' ], [ 'is', 'a', 'high-level,', 'general-purpose,' ], [ 'a', 'high-level,', 'general-purpose,', 'interpreted,' ], [ 'high-level,', 'general-purpose,', 'interpreted,', 'dynamic' ], [ 'general-purpose,', 'interpreted,', 'dynamic', 'programming' ], [ 'interpreted,', 'dynamic', 'programming', 'language.' ] ], '3' => [ [ 'Perl', 'is', 'a' ], [ 'is', 'a', 'high-level,' ], [ 'a', 'high-level,', 'general-purpose,' ], [ 'high-level,', 'general-purpose,', 'interpreted,' ], [ 'general-purpose,', 'interpreted,', 'dynamic' ], [ 'interpreted,', 'dynamic', 'programming' ], [ 'dynamic', 'programming', 'language.' ] ], '2' => [ [ 'Perl', 'is' ], [ 'is', 'a' ], [ 'a', 'high-level,' ], [ 'high-level,', 'general-purpose,' ], [ 'general-purpose,', 'interpreted,' ], [ 'interpreted,', 'dynamic' ], [ 'dynamic', 'programming' ], [ 'programming', 'language.' ] ] };
update: <cough> Had an "off by two" error. :-)

In reply to Re: extract phrases of n-words length by wfsp
in thread extract phrases of n-words length by arun_kom

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.