use strict; use warnings; my (%count, $str, $size, $min); $str = 'abczdefzabcghijklzabczaerabrtyuabcethdauthabkudiabc'; $size = 3; # Size of substrings to look for $min = 2; # Minimum number of times substring appears $count{substr($str, $_, $size)}++ for 0..(length($str) - $size); for (sort {$count{$b} <=> $count{$a} || $a cmp $b} keys %count) { last if $count{$_} < $min; print "$_ :"; print ' '.(pos($str)-$size) while $str =~ /$_/g; print " ($count{$_} matches)\n"; }