________________________________
From: Rob Coops <[EMAIL PROTECTED]>
To: beginners@perl.org
Sent: Wednesday, October 22, 2008 3:24:12 PM
Subject: Re: Conditional replace

On Wed, Oct 22, 2008 at 3:38 PM, Brian <[EMAIL PROTECTED]> wrote:

> Hi
>
> ARGV0 will = AB7Z001
> ARGV1 will = AB7Z002
> ARGV2 will = 01/01/1900
>
> I would like to read a file, locate AB7Z001 (but not AB7Z0011,  so a space
> at position 8 in string )
> Upon location of value in argv0 replace it with argv1.
> Then, at the first instance of a date replace it with argv2.
> Then, at the next instance of a date, replace it with "today's date".
> There will only be one instance of AB7Z001 in the file, but/so, is there a
> way to stop checking and go on to output after this "one event"
>
> So that,
>
> AB7Z001 blahblahblah 01/01/1485 moreblahblah evenmoreblahblah 01/01/1999
> AB7Z0011 blahblahblah 01/01/1485 moreblahblah evenmoreblahblah 01/01/1999
>
> will be
>
> AB7Z001 blahblahblah 01/01/1900 moreblahblah evenmoreblahblah 22/10/2008
> AB7Z0011 blahblahblah 01/01/1485 moreblahblah evenmoreblahblah 01/01/1999
>
> open (IN, "+<dummy.txt");
> @file = <IN>;
> seek IN,0,0;
> foreach $file (@file){
> $file =~ s/............................./g; <----------- ??
>
> print IN $file;
> }
> close IN;
>
> Thanks
> Brian
>
>
>


You are reading the the whole file into an array in one big gulp... *(@file
= <IN>;)* are you sure you will not at some point have a file that is a few
Gigabytes in size causing the whole thing to roll over and die?

At this point, I'm only looking at using a small file, but a good point and one 
that I will bear in mind for the future.

Anyway, I'll stay with the big greedy dump it all in an array for now.

<snip>

That should do the trick I guess.
What you where trying to do with *seek IN,0,0;* does not work unless you are
happy not to see the rest of the file after $ARGV0 has been found, because
you wanted to stop looping through the file then. If you didn't want to stop
once the condition has been met that might just do the trick (never tried it
my self so I cannot confirm)

Understood, I'll play with the 2 options.

Thank you, very much appreciated.
regards
Brian


      

Reply via email to