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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|