I have two CSV files that look like this:
File1:
Name Type ID Language Text Comment
App_Item125 normal 1 en init [empty]
App_Item167 good 66 fr init
App_Item1222 none 23 it init
File2:
Name SID
App_Item122 SID_Foo
App_Item1345 SID_Bar
App_Item1222 SID_None
I want to write a Perl script that inserts the SIDs from File2 into File1. SIDs should be put in the 5th column (named "Comment") of the row with the matching "Name" field. The first column of both files contains the same data but in a different order.
This is the code I wrote:
my @File2 = "File2.csv";
open(FILE2, @File2) || die "Could not open file";
my @File1 ="File1.csv";
open(FILE1, @File1) || die "Could not open file";
my $SID;
while( <FILE2>) {
if($File2[0] eq $File1[0]) { # this is probably totally wrong: if the elements of the first columns of file2 match with the ones of File1
$SID = $File1[1]; #this is the SID in the 2nd column of File2
print $File2[5] $SID; #print that matching Element into column 6 of File2
}
}
close FILE1;
close FILE3;
close FILE2;
My question is, how do I define columns and lines in CSV files in Perl?
open
and lexical filehandles. Include the filename and error string in yourdie
statements.my $file = 'foo.csv'; open my $fh, '<', $file or die "$file: $!";
- ThisSuitIsBlackNot