On Thu, 2003-10-30 at 12:26, 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.
Is this remotely close to what you're looking for?
# read data on stdin. collect output on stdout.
while (<>) {
chomp;
@line = split(',');
# interested in records stating with digits
if (/^\d/) {
# update fields 4, 5, 13.
@line[3,4,12] = ('one', 'two', 'three');
print join(',',@line), "\n";
} else {
print "$_\n";
}
}
Alternative to print you could push onto the array of records as you
have been.
>
> Can some one shed some light on how I can do this without updating a
> single line at a time?
>
> Thanks.
>
> Michael
>
>
>
>
>
> Michael L. Bowden
> Coordination of Automation and Access Services
> Harrisburg Area Community College
> e: [EMAIL PROTECTED]
--
Dale Bewley - [EMAIL PROTECTED] - 530-752-1202
Unix Administrator - Shields Library - UC Davis