for my $i (0..$N-1) { for my $j ($i..$N-1) { my $substr = substr($s, $i, $j-$i+1); if ( exists( $hash{$substr} ) { ... } } }