As long as you are not allowing overlaps, an alternative to toolic's regex solution would be to use unpack.

johngg@shiraz:~/perl/Monks > perl -Mstrict -Mwarnings -E ' my $str = q{BEBEBEHUHUHUJJFAFALL}; my %cnt; $cnt{ $_ } ++ for unpack q{(a2)*}, $str; say qq{$_ -> $cnt{ $_ }} for sort keys %cnt;' BE -> 3 FA -> 2 HU -> 3 JJ -> 1 LL -> 1

I hope this is of interest.

Update: A bit of a lash-up allowing for overlaps.

johngg@shiraz:~/perl/Monks > perl -Mstrict -Mwarnings -E ' my $str = q{BEBEBEHUHUHUJJFAFALL}; my %cnt; my $len = shift || 2; my $overlap = shift || 0; my $fmt = $overlap ? qq{(a${len}X@{ [ $len - 1 ] })*} : qq{(a${len})*}; $cnt{ $_ } ++ for grep { length == $len } unpack $fmt, $str; say qq{$_ -> $cnt{ $_ }} for sort keys %cnt;' BE -> 3 FA -> 2 HU -> 3 JJ -> 1 LL -> 1 johngg@shiraz:~/perl/Monks > perl -Mstrict -Mwarnings -E ' my $str = q{BEBEBEHUHUHUJJFAFALL}; my %cnt; my $len = shift || 2; my $overlap = shift || 0; my $fmt = $overlap ? qq{(a${len}X@{ [ $len - 1 ] })*} : qq{(a${len})*}; $cnt{ $_ } ++ for grep { length == $len } unpack $fmt, $str; say qq{$_ -> $cnt{ $_ }} for sort keys %cnt;' 2 1 AF -> 1 AL -> 1 BE -> 3 EB -> 2 EH -> 1 FA -> 2 HU -> 3 JF -> 1 JJ -> 1 LL -> 1 UH -> 2 UJ -> 1 johngg@shiraz:~/perl/Monks > perl -Mstrict -Mwarnings -E ' my $str = q{BEBEBEHUHUHUJJFAFALL}; my %cnt; my $len = shift || 2; my $overlap = shift || 0; my $fmt = $overlap ? qq{(a${len}X@{ [ $len - 1 ] })*} : qq{(a${len})*}; $cnt{ $_ } ++ for grep { length == $len } unpack $fmt, $str; say qq{$_ -> $cnt{ $_ }} for sort keys %cnt;' 3 AFA -> 1 BEB -> 1 EBE -> 1 HUH -> 1 JJF -> 1 UHU -> 1 johngg@shiraz:~/perl/Monks > perl -Mstrict -Mwarnings -E ' my $str = q{BEBEBEHUHUHUJJFAFALL}; my %cnt; my $len = shift || 2; my $overlap = shift || 0; my $fmt = $overlap ? qq{(a${len}X@{ [ $len - 1 ] })*} : qq{(a${len})*}; $cnt{ $_ } ++ for grep { length == $len } unpack $fmt, $str; say qq{$_ -> $cnt{ $_ }} for sort keys %cnt;' 3 1 AFA -> 1 ALL -> 1 BEB -> 2 BEH -> 1 EBE -> 2 EHU -> 1 FAF -> 1 FAL -> 1 HUH -> 2 HUJ -> 1 JFA -> 1 JJF -> 1 UHU -> 2 UJJ -> 1

Cheers,

JohnGG


In reply to Re: Counting words by johngg
in thread Counting words by bisimen

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.