use strict; use warnings; # Warning! # This code is untested, as I don't have any representative test data # to work with. As such, it may contain bugs, logical faults, and othe +r # subtleties. However, it should get the idea along. # Warning! # Because your specifications were a little vague at best, I had to do # some guesswork. One of the things I guesses, was that by # > search for the pattern $abc # you meant you want to match against the pattern as stored in the # variable $abc, as opposed to matching against the literal sring '$ab +c', # that is, dollar, small letter a, small letter b, small letter c. # Another thing I had to guess was that those strings "yyx" and "yyw", # as from your sample input, are likely candidates to be the value of # this variable $abc. # This code is written with the above assumptions in mind. # Warning! # In your original post, you defined, more or less, what the needle # is that we're looking for ($abc), and what the haystack is we're # looking in (file2), but not what you want to happen when something # is actually found. my $length = 250; my @w00t = ("Roses are red,", " Violets are blue,", "This lame-ass po +em,", " doesn't rhyme."); # Let's open those files. open my $fhConditions, "<", "somethinglikethis.txt" or die "Epic Fail: + $!"; open my $fhCharacters, "<", "file2" or die "OMG Fail: $!"; while (my $line = <$fhConditions>) { chomp $line; my ($where, $abc, $position) = split(' ', $line); # Uncomment +for Case 2 $position -= $length if $where == 0; $position = 0 if $position < 0; seek $fhCharacters, $position, 0; my $data; read $fhCharacters, $data, $length; if ($data =~ m/$abc/) { print $w00t[rand @w00t], "\n"; } }

In reply to Re: matchin a pattern 250 characters up and down stream by muba
in thread matchin a pattern 250 characters up and down stream by Anonymous Monk

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.