use threads ; use feature "say"; use warnings; my $input_file = "text"; #Contains a single lined string without any newline character open my $hd, "<", $input_file or die "Couldn't open '$input_file' - $!"; my $text=<$hd>; chomp($text); my $window=5; #Length of the window my $i=0; LOOP:seek $hd,$i,0; #Here seek and read give us the first $window lengthed substing runned over loop read ($hd,my $substring,$window); my %hash; if(length($substring)==$window) { $hash{$substring}=$i; #$substring pushed into a hash, here I have avoided array has there may be repeated substrings in the $text $i++; goto LOOP; } my $count = keys %hash; my $j=0; foreach my $pattern (keys %hash) { if($j<=$count) { my $thr="thread".$j; $$thr=threads->create(\&position,$text,$pattern); $j++; } } for($ii=0;$ii<=$count;$ii++) { $thr="thread".$ii; print $thr,"\n"; $$thr->join(); } sub position { my ($text,$pattern) = @_ ; my $offset = 0; print "\n$pattern ($window)\n"; print '~' x $window,"\n"; my $pos=index $text,$pattern,$offset; while ($pos != -1) { print $pos+1," to ",$pos+$window,"\n"; $offset = $pos + 99; $pos = index($text, $pattern, $offset); } }