#!/usr/bin/perl -w use strict; my $line; my %count; while ( defined ( $line = <> ) ) { chomp $line; for my $a ( 1 .. length ( $line ) ) { for my $b ( 1 .. ( length ( $line ) + 1 - $a ) ) { my $out = substr ( $line, $a - 1, $b ); next unless $count{ $out }; # so you don't have to use uniq next if ( $count{ $out } = () = $line =~ /$out/g ) > 1; print "length: $b digits: $out quantity: $count{ $out }"; } } }