Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello,
I've a text file about 5000 lines and I need to retrive some columns information from a table which also has 5000 records and add these information to each line of the original file. My question is how to find a way to attatch these information to each line in the original file.

thanks!
yi

edited: Wed Jun 26 15:37:04 2002 by jeffa - title change and added formatting

  • Comment on update every line of a text file (was: file)

Replies are listed 'Best First'.
Re: file
by kvale (Monsignor) on Jun 25, 2002 at 23:07 UTC
    To update a file, you open it for reading, read line by line, update the line, and write the line out to a new file. Assume the @table holds the data to add. Then you could use something like
    open OLD, "<old.txt" or die "Could not open old.txt: $!"; open NEW, ">new.txt" or die "Could not open new.txt: $!"; my $i = 0; while (<OLD>) { chomp; $_ .= $table[ $i++ ]; print NEW "$_\n"; } close OLD; close NEW;
    Nota bene: it is better to entitle your questions with something more descriptive than "file", say, "Editing a file in place", or "Updating a flat file database".

    -Mark