in reply to Re^4: search pattern and arrays
in thread search pattern and arrays

Here you go:

$ 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
    John ur a genius ... ill let u know after running.... thanks, Mercury
Re^6: search pattern and arrays
by mercuryshipz (Acolyte) on Jan 24, 2008 at 17:44 UTC
    test.txt 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 76 wireless cards produced #!/usr/bin/perl # use strict; use warnings; use List::Util q{first}; sub search_phrase{ my ( $inFile, @phrases ) = @_; 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; } } $file_n = "test.txt"; $phrase1 = "total Laptops produced:"; $phrase2 = "total monitors produced:"; $phrase3 = "total cpu sold:"; &search_phrase($file_n, $phrase1, $phrase2, $phrase3);
    the scripts works great....
    i have changed this as a function
    but a few more additions John
    i have updated a line in test.txt
    1. the return number which we want could be even at the beginning of the line also.

    2. If the last phrase is not found it has to return -1 as the value
    3. one more thing is if the same phrase is passed twice, it has return the last phrase's value.
    For eg.
    $phrase1 = "total cpu sold:";
    $phrase2 = "total mice produced:";
    $phrase3 = "total cpu sold:";

    phrase3 's value has to be returned not both...
    for example in our file

    45 is to be returned, not both (57 and 45)
    3. Could u add some comments if possible, to make it clear.

    thanks a lot,

    Mercury.
      Mercury, I'm sorry but I don't have a lot of time to reply just now as my system seems to be gradually failing since a power cut borked something; now it can't even bring up a terminal session. I think a re-install may be in order.

      Just a few of quick points:

      • It is good practice for code maintenance to maintain a consistent indenting scheme so it might be an idea, having placed some of the code in a subroutine, to indent it further to aid legibility.
      • Don't preface your subroutine calls with an ampersand (&). That has a specific meaning which, in general, you don't want, see perlsub.
      • Now that you have employed a subroutine you will need it to return something and actually assign that to a variable, my $result = search_phrase( ... );
      • To cope with numbers at the start of your line, and to cope with not finding the phrase, you will need to modify the line that starts with the print, perhaps changing to an if ( cond ) { ... } else { ... } and change the regular expression to cater for digits either at the beginning or the end. Hint: use the * quantifier (0 or more) rather than the + (1 or more).
      I hope this short reply is enough to help you move forward. I hope my system lasts long enough for me to click the <create> button ;-)

      Cheers,

      JohnGG

        thanks John ... not a problem.... these suggestions will definetly be a great help...

        thanks,
        Mercury.