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 ;)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.