Here a variation of my former code which allows to calculate a range of n-grams simultaneously.

Please note that instead of printing "$n:" you could also push to an @array or print to a filehandle.

The filehandle or arrayref or whatever target stream could be held in a hash $ngrams{$n} ....

... this should be among the fastest solutions ¹

Also it's easy to change this from range to list of n-grams, just keep $n_max right.

HTH

use strict; use warnings; my @cache; my $n_min=2; my $n_max=6; while (my $line =<DATA>) { chomp $line; push @cache, $line; for my $n ($n_min..$n_max) { print "$n: @cache[-$n .. -1]\n" # last n of cache if @cache >= $n; } shift @cache if @cache == $n_max; # keep cache at max size } __DATA__ One Two Three Four Five Six Seven Eight Nine Ten

out
2: One Two 2: Two Three 3: One Two Three 2: Three Four 3: Two Three Four 4: One Two Three Four 2: Four Five 3: Three Four Five 4: Two Three Four Five 5: One Two Three Four Five 2: Five Six 3: Four Five Six 4: Three Four Five Six 5: Two Three Four Five Six 6: One Two Three Four Five Six 2: Six Seven 3: Five Six Seven 4: Four Five Six Seven 5: Three Four Five Six Seven 6: Two Three Four Five Six Seven 2: Seven Eight 3: Six Seven Eight 4: Five Six Seven Eight 5: Four Five Six Seven Eight 6: Three Four Five Six Seven Eight 2: Eight Nine 3: Seven Eight Nine 4: Six Seven Eight Nine 5: Five Six Seven Eight Nine 6: Four Five Six Seven Eight Nine 2: Nine Ten 3: Eight Nine Ten 4: Seven Eight Nine Ten 5: Six Seven Eight Nine Ten 6: Five Six Seven Eight Nine Ten

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

¹) well you could have a sliding window and a regex to be even faster ;)

update

simplified range code from @cache-$n ..$#cache to -$n .. -1


In reply to Re: Create n-grams from tokenized text file (multiple n-grams in parallel) by LanX
in thread Create n-grams from tokenized text file by Anonymous Monk

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.