Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
If/when someone posts a Benchmark in this thread, I would predict that a substr() approach will come out ahead. But you might also want to look at Text::Ngram -- maybe instead of an array of 3-grams, you actually want a hash, which has each 3-gram as a hash key, and its frequency of occurrence in the string as the value. From the module's man page:
use Text::Ngram qw(ngram_counts add_to_counts); my $text = "abcdefghijklmnop"; my $hash_r = ngram_counts($text, 3); # Window size = 3 # $hash_r => { abc => 1, bcd => 1, ... }
Of course, if you need to keep track of the order in which the 3-grams occur, having them stored as hash keys would not be very useful.

(BTW, you said "I need to take a string and find the number of 3 contiguous letters" -- to be precise, the number of 3-grams in a string is simply  length( $string ) - 2 ;)

In reply to Re: finding number of contiguous letters by graff
in thread finding number of contiguous letters by Marknel

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (7)
    As of 2020-10-27 21:10 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (258 votes). Check out past polls.