Michael Bowden wrote:

Hi folks:

I first want to apologize for this post.  I think that during the fall
back, I lost a few brain cells.

I am working on a Perl script to update a data file.  I load my data
into an array.  I want to cycle through the array and make changes. Then
output the array to a file.  In the past, I would open the database,
read in the lines, and output each line as I updated the elements in the
array.  In these scripts, I was only updating, one element in the array.
Now I need to update several elements in the array before writing
everything to a file.  If I write the program as I have in the past, I
would be using a lot of i/o reading the data into the array, changing
one element and then outputting the data to a file and repeating the
steps over and over until all the changes have been made.  In some
cases, I will be changing 20 to 30 elements in the array.  So I need
some help.

I read in an array from a file. It looks something like this:

200044455669,,abcdefg,7777777,f00,f00,f00,f00,a01,b23,c45,d00,e10,no,no,no,no,no,
200011122333,,hikls,2222223,f32,f43,f00,f00,a04,b06,c08,d03,e09,no,no,no,no,no,
mrgreenjeans,,pqrstuv,44442244,f20,f00,f00,f00,a06,b08,c10,d10,e00,no,no,no,no,no,
20006654987,,zzzxyxz,33344434,f03,f00,f00,f00,a00,b12,c12,d13,e14,no,no,no,no,no,

Each line represents one element in the array. So $array[0] equal 200044455669,,abcdefg,7777777,f00,f00,f00,f00,a01,b23,c45,d00,e10,no,no,no,no,no,.

I want to update the 4th, 5th, and 13th subelements in $array[0],
$array[1] and $array[4].  Then I want to write the updated @array back
to a file.

Can some one shed some light on how I can do this without updating a
single line at a time?


Hi Michael,

Is it really too much i/o to do things as you have described? It sounds like you are reading in the entire file once, and then writing it out once. I cannot imagine how you could do it better than that without using a special data file structure (DBM files etc).

Here is what I think you are doing (roughly):

open(INFILE, "<somefile);
open(OUTFILE, ">someotherfile");
while $line (<INFILE>) {
 # perform some operation on the line of data
 $line =~ s|someregularexpression|somereplacement|;
 print OUTFILE $line;
}
close(INFILE);
close(OUTFILE);
# replace the INFILE with the OUTFILE by renaming OUTFILE.

Is that what you are doing? Or something else? I think that is about as effecient as you can get with i/o without using a datafile on disk that has an index (like DBM etc).

Keep in mind that by default your i/o is buffered so PERL won't be writing any data at all until it feels it has enough data to make the disk i/o worthwhile. That helps to prevent the system from having too many tiny writes to disk.

--
Michael McDonnell, GCIA
[EMAIL PROTECTED]



Reply via email to