I think this is what you are after although I may be misreading your post. If I understand correctly, you search for your phases in a certain order and you want to find the first occurence of each phrase ignoring any phrases that are out of sequence, e.g. a "phrase2" that is before the first "phrase1" is ignored.

To do this I read the lines into an array. If your file is very large this might not be feasible. The adjustment to $lineNo is because arrays are zero-based and I'm assuming you number your lines from 1.

use strict; use warnings; use List::Util q{first}; use Data::Dumper; open my $inFH, q{<}, \ <<EOD or die qq{open: $!\n}; 1:gash line 2:phrase4 3:akjdakj 4:fwefkwe 5:phrase5 6:phrase1 7:adsfwfw 8:phrase3 9:phrase5 10:jkjd wsekjw wiu 11:phrase2 12:wewefwefwf 13:another line 14:dsjwjk 15:adsfwfw 16:phrase3 17:another line 18:adsfwfw 19:phrase5 20:phrase6 21:ertgerher EOD my @lines = <$inFH>; close $inFH or die qq{close: $!\n}; my @phrases = map { qq{phrase$_} } 1 .. 6; my $cumulativeOffset = 0; foreach my $phrase ( @phrases ) { my $rxPhrase = qr{$phrase}; my $lineNo = first { $lines[ $_ ] =~ $rxPhrase } 0 .. $#lines; unless ( defined $lineNo ) { print qq{$phrase: not found in sequence\n}; next; } $lineNo ++; $cumulativeOffset += $lineNo; print qq{$phrase: $cumulativeOffset\n}; splice @lines, 0, $lineNo; }

The output.

phrase1: 6 phrase2: 11 phrase3: 16 phrase4: not found in sequence phrase5: 19 phrase6: 20

I hope I have guessed right and this is of use.

Cheers,

JohnGG


In reply to Re: search pattern and arrays by johngg
in thread search pattern and arrays by mercuryshipz

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.