On Aug 14, 5:48 am, [EMAIL PROTECTED] (Rob Dixon) wrote:
> - <STDIN> isn't an implicit array. Where did you read that? It's a read
> operattion on the STDIN file handle.

> Does this program help?
>
> Rob

I was unclear, sorry about that.
You're correct I meant there is an implicit 'loop' (not array!) with
$ perl -pe ';' test.txt or $ perl -e 'while(<>){print;}' test.txt

Thank you very much Rob , this is a great script and help me
understand many things.
I learned lots from reworking it and found a better solution than
manual labor!

I'm sure to learn more from Xavier and John's input too.

I modified it to just get the part of the previous line, but is there
a way get any line besides the previous when one doesn't know the
context range ie -C[0-9]{1,}  when the context is variable (see
revised script)

That is why I tried to get the exact line numbers of nearby matched
lines then collect them in an array and then loop over the linenumber
elements with substitution lines which are in variable context to the
matched line. I finally found out that there is no index using a
foreach loop. :-(

Here's your revised program:
#!/usr/bin/env perl
use strict;
use warnings;
my $prev = ''; #the matched line that is one line previous to the
context line.
my $prevpattern = ''; #a substring of the previous line
my $linepattern = "tag"; #lines that need substitution have 'tag' or
some pattern aka the context line needing substitution

while (<DATA>) {
  chomp;
  if (/$linepattern/) {
        $prevpattern = substr($prev,index($prev," ")+1,10); #don't want the
whole previous line just the last word(s), either cat or dog or better
yet any substring within the matched line
    s/this/$prevpattern/;
  }
  else {
    $prev= $_;
  }
  print "$_\n";
}

__END__
animal cat
tag this line
animal dog
tag this line
blue ideas snore peacefully #this line won't interfere with
substitution because it is not between the matched previous line and
its context line
green thoughts wade holy#this line won't interfere with substitution
because it is not between the matched previous line and its context
line
animal cat
ugh thee line that breaks the script # the next 'tag' line should be
'tag cat line' not 'tag now line'
more
now
tag this line
tool hammer
tag this line

I get a good result, except where the context range is variable as at
lines 7 to 11 where line 7, the match line with my pattern 'cat' is
not previous to line 11, the line where the substitution suppose to
occur:

Result of above program:
     1  animal cat
     2  tag cat line
     3  animal dog
     4  tag dog line
     5  blue ideas snore peacefully #this line won't interfere with
substitution because it is not between the matched previous line and
its context line
     6  green thoughts wade holy#this line won't interfere with
substitution because it is not between the matched previous line and
its context line
     7  animal cat
     8  ugh thee line that breaks the script # the next 'tag' line
should be 'tag cat line' not 'tag now line'
     9  more
    10  now
    11  tag now line
    12  tool hammer
    13  tag hammer line


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to