[please keep on list]
[especially when the person you're e-mailing is out of town ;) ]
- Show quoted text -


On Wed, 24 Nov 2004 09:17:18 -0500, FlashMX <[EMAIL PROTECTED]> wrote:
>
> Hi Jay,
>
> I'm getting mixed reviews on how to do this script. I've been told by dumping 
> the input file to an array could slow things down because it uses up memory 
> and swap. There could be multiple
> users running the script at the same time which, if the files are large, 
> would not be good. So I've redone my script a little. I've been able to 
> search and replace one line in the output file but I'm
> having trouble trying to figure out how to do the replace on the second match.
>
> I have the book "O'Rielly Perl Cookbook". It has alot of examples but I'm 
> getting really confused on how to implement the examples into what I want to 
> achieve.
>
> Could you help me out?
>
> I've attached a zip file of my code and the external file being read in. I 
> need to search and replace "0 AAA" with "0 AAA BBB" which is working just 
> fine. My problem is how to get the second
> search and replace to change the "first" occurrence of "XXX" after the "0 AAA 
> BBB" search and replace is completed but no "XXX" afterwards to "111 XXX"
>
> If you look in "file.in" which is the input file and the file "results.out" 
> which I just keyed in manually for reference. This is what I want outputted. 
> I'm really getting confused. I'm being told don't use
> arrays because it slow things down. Use grep, map...etc...etc.
>

[attachment removed for list, your code below:]

>#!/usr/local/bin/perl
>use strict;
>
>my $ifile = "file.in";
>my $ofile = "file.out";
>my $line;
>
>open (INFILE, "< $ifile");
>       open (OUTFILE, "> $ofile");
>               foreach $line (<INFILE>) {
>                       if (grep(/0 AAA /i, $line)) {
>                               $line =~ s/0 AAA /0 AAA BBB /gi;
>                       }
>               print OUTFILE $line;
>               }
>       close(OUTFILE);
>close(INFILE);
>
>#rename($ofile, $ifile);
__END__

If you're not reading into a variable, while will almost certainly be
faster here, and there is no reason to use grep on anything but a
list, so:

while (my $line = <INPUT>){
  s/0 AAA/0 AAA BBB/gi;
}

Your cuurent code runs the regex once on every line, and twice on
lines that match.  The substitution operator exitsts tosimultanteously
find and replace.  If the first part doesn't match, the second doesn't
execute.

I'm not sure I know what you're getting at here, but I think I have
some idea.  I'm assuming that the following is what we're more or less
dealing with, based on what you've said:

Input:
101 101 000 011
0 AAA
010 001 000 001
XXX
000 000 000 110
XXX
000
XXX
0 AAA

Desired output:
101 101 000 011
0 AAA BBB
010 001 000 001
111 XXX
000 000 000 110
XXX
000
XXX
0 AAA BBB

you have two match criteris here, we'll call them m1 and m2.  what you
want to do is find m1, do something, go on to m2, do something, go to
the next m1, do something, etc...

There are a couple of ways to do this.  The easiest would proabably be
to use a counter:

#!/usr/local/bin/perl -w
use strict;

my $ifile = "file.in";
my $ofile = "file.out";
my $line;
my $m1 = 0;
my $m2 = 0;

open (INFILE, "< $ifile");
open (OUTFILE, "> $ofile");
while ($line = <INFILE>) {
    chomp $line;
    $m1++ if $line =~ s/0 AAA/0 AAA BBB/i;
    if ($m2 < $m1) {
         $m2++ if $line =~ s/XXX/111 XXX/i;
    }
   print OUTFILE "$line\n";
}
close(OUTFILE);
close(INFILE);

#rename($ofile, $ifile);
__END__

HTH,

--jay savage

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


Reply via email to