This seems to work. Run it on a terminal 100+ wide to see the output properly; the wrapping screws it up.

#! perl -slw use strict; sub search { my( $haystack, $needle, $pos ) = @_; my $l = length $needle; my( @res1, @res2 ); return( $pos-1, 0, $l ) if $pos = 1+index $haystack, $needle; substr( $haystack, 0, $_ ) eq substr( $needle, -$_ ) and @res1 = ( 0, $l-$_, $_ ), last for reverse 1 .. $l-1; substr( $haystack, $_-$l ) eq substr( $needle, 0, $l-$_ ) and @res2 = ( length( $haystack )-( $l-$_), -$_, $l-$_ ), las +t for 1 .. $l-1; return unless @res1 or @res2; return @res1 unless @res2; return @res2 unless @res1; return $res1[2] > $res2[2] ? @res1 : @res2; } my $haystack = 'GAATGTTTTAGCAATCTCTTTCTGTCATGAATCCATGGCAGTGACCATACTAAT +GGTGACTGCCATTGATGGAGGGAGACACA'; my @needles = ( 'NOT FOUND', 'CTGGATAAGAATGTTTTAGCAATCTCTT', 'CTGCCATTGATGGAGGGAGACACAACGTACGT', 'CATGAATCCATGGCAGTGACCATACTAATGGTGACTG', 'AGGGAGACACAGAATGTTTTAG', 'AGGGAGACACAGAATGTTTTAGC', ); $, = ' '; for my $needle ( @needles ) { my( $hOffset, $nOffset, $l ) = search $haystack, $needle; print "No match found in '$haystack'\n for '$needle'\n" and next unless defined $hOffset; printf "%s%s\n%s%s\n\n", ' 'x$nOffset, $haystack, ' 'x$hOffset, $needle; } __END__ c:\test>579215 No match found in 'GAATGTTTTAGCAATCTCTTTCTGTCATGAATCCATGGCAGTGACCATACT +AATGGTGACTGCCATTGATGGAGGGAGACACA' for 'NOT FOUND' GAATGTTTTAGCAATCTCTTTCTGTCATGAATCCATGGCAGTGACCATACTAATGGTGACTG +CCATTGATGGAGGGAGACACA CTGGATAAGAATGTTTTAGCAATCTCTT GAATGTTTTAGCAATCTCTTTCTGTCATGAATCCATGGCAGTGACCATACTAATGGTGACTGCCATTGAT +GGAGGGAGACACA CTGCCATTGAT +GGAGGGAGACACAACGTACGT GAATGTTTTAGCAATCTCTTTCTGTCATGAATCCATGGCAGTGACCATACTAATGGTGACTGCCATTGAT +GGAGGGAGACACA CATGAATCCATGGCAGTGACCATACTAATGGTGACTG GAATGTTTTAGCAATCTCTTTCTGTCATGAATCCATGGCAGTGACCATACTAATGGTGACTGCCATTGAT +GGAGGGAGACACA + AGGGAGACACAGAATGTTTTAG GAATGTTTTAGCAATCTCTTTCTGTCATGAATCCATGGCAGTGACCATACTAATGGTGA +CTGCCATTGATGGAGGGAGACACA AGGGAGACACAGAATGTTTTAGC

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Searching for a word that may only exist in part by BrowserUk
in thread Searching for a word that may only exist in part by seaver

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.