in reply to Re^4: PERL STRING QUESTION
in thread PERL STRING QUESTION

Can you please tell me how to print the location/position of match in your sequence.

If I understand the question, you could use split instead of a while loop:

... my $seq = <$fh>; # entire file is now in $seq; $seq =~ tr/\n//d; # remove all newlines open( my $out, ">", "write.txt" ) or die "Cannot create write.txt: $!\ +n"; my @chunks = split /(..$find..)/, $seq; my $offset = 0; for my $chunk ( @chunks ) { if ( $chunk =~ /(..)$find(..)/ ) { printf( "%s occurs at character offset %d, between %s and %s\n +", $find, $offset + 2, $1, $2 ); } $offset += length( $chunk ); }
Of course, if you want character offsets to be "accurate" relative to the input file, you'll have a problem: any newlines in the original have been deleted, and are not being counted in the offset values being printed as output. But maybe you just want offsets to be accurate relative to the non-whitespace content, or something like that?

If you want other information about the context around each "hit", you should be able to work out what to do in that for loop for the chunks between the hits.