Ayodya
Ayodya

Reputation: 1

How can I copy columns from several files into the same output file using Perl

This is my problem.

I need to copy 2 columns each from 7 different files to the same output file. All input and output files are CSV files. And I need to add each new pair of columns beside the columns that have already been copied, so that at the end the output file has 14 columns.

I believe I cannot use

open(FILEHANDLE,">>file.csv").

Also all 7 CSV files have nearlly 20,000 rows each, therefore I'm reading and writing the files line by line.

It would be a great help if you could give me an idea as to what I should do.

Thanx a lot in advance.

Upvotes: 0

Views: 416

Answers (2)

Dallaylaen
Dallaylaen

Reputation: 5308

Text::CSV is probably the way to access CSV files.

You could define a csv handler for each file (including output), use getline or getline_hr (returns hashref) methods to fetch data, combine it into arrayrefs, than use print.

Upvotes: 1

Brian Roach
Brian Roach

Reputation: 76898

Provided that your lines are 1:1 (Meaning you're combining data from line 1 of File_1, File_2, etc):

  • open all 7 files for input
  • open output file
  • read line of data from all input files
  • write line of combined data to output file

Upvotes: 2

Related Questions