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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |