in reply to Re^4: search pattern and arrays
in thread search pattern and arrays
$ cat spw663850.dat total Laptops produced: 60 total cpu sold: 57 total mice produced: 40 total cpu sold: 45 total Laptops produced: 68 total mice produced: 48 total cpu sold: 51 total printers produced: 19 total monitors produced: 149 $ $ $ cat spw663850 #!/usr/bin/perl # use strict; use warnings; use List::Util q{first}; my ( $inFile, @phrases ) = @ARGV; my $lastPhrase = $phrases[ -1 ]; open my $inFH, q{<}, $inFile or die qq{open: $inFile: $!\n}; my @lines = <$inFH>; close $inFH or die qq{close: $!\n}; foreach my $phrase ( @phrases ) { my $rxPhrase = qr{\Q$phrase\E}; my $lineNo = first { $lines[ $_ ] =~ $rxPhrase } 0 .. $#lines; unless ( defined $lineNo ) { print qq{$phrase: not found in sequence\n}; next; } print qq{$1\n} if $lines[ $lineNo ] =~ m{\Q$lastPhrase\E\s*(\d+)}; $lineNo ++; splice @lines, 0, $lineNo; } $ $ $ ./spw663850 spw663850.dat 'total Lap tops produced:' 'total mice pro +duced:' 'total cpu sold:' 45 $
Feel free to ask if the script is not clear. I cleaned up the data file so that none of the lines had spaced before the colons just for consistency.
Cheers,
JohnGG
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: search pattern and arrays
by Anonymous Monk on Jan 24, 2008 at 01:46 UTC | |
|
Re^6: search pattern and arrays
by mercuryshipz (Acolyte) on Jan 24, 2008 at 17:44 UTC | |
by johngg (Canon) on Jan 25, 2008 at 00:00 UTC | |
by mercuryshipz (Acolyte) on Jan 25, 2008 at 02:44 UTC |