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 other # 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 '$abc', # 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 poem,", " 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"; } }