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.


In reply to Re^5: PERL STRING QUESTION by graff
in thread PERL STRING QUESTION by vikuuu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.