in reply to search pattern and arrays
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: search pattern and arrays
by Anonymous Monk on Jan 23, 2008 at 21:30 UTC | |
by johngg (Canon) on Jan 23, 2008 at 22:21 UTC | |
by mercuryshipz (Acolyte) on Jan 23, 2008 at 22:34 UTC | |
by johngg (Canon) on Jan 24, 2008 at 00:25 UTC | |
by Anonymous Monk on Jan 24, 2008 at 01:46 UTC | |
by mercuryshipz (Acolyte) on Jan 24, 2008 at 17:44 UTC | |
|