bluray has asked for the wisdom of the Perl Monks concerning the following question:
I was trying to match the first column in three files (each file has 6 columns) and get the matching results in the output file. In this case, the first five columns in the three matching files are the same, but there is a difference in the last column for the three files. So, I tried to concatanate the last columns from the input files also in the output file. But, I was stuck with an error "expected fields to be an array ref at line 74 <$FILE1> line 2. I tried to make it an array ref, then the output file first column has the following repeated (ARRAY(0x141ecf8) ARRAY(0x141edd0) ARRAY(0x141ee48)). There was nothing else in the other columns.
#!/usr/bin/perl -w use strict; use warnings; use Text::CSV_XS; open (my $FILE1, '<', "inputfile1.csv") or die "cannot open file1 $!\n +"; open (my $FILE2, '<', "inputfile2.csv") or die "cannot open file2 $!\n +"; open (my $FILE3, '<', "inputfile3.csv") or die "cannot open file3 $!\n +"; open (my $FILE4, '>', "Outputfile.csv") or die "cannot open file4 $!\n +"; my $csv = Text::CSV_XS->new ({ binary => 1, eol => $/ , sep_char => "\t", always_quote =>1}); my @columnheadings=split(/\t/,<$FILE1>); <$FILE2>; <$FILE3>; push (@columnheadings,('InFCDAC','InFCTriple')); my $headings=join("\t",@columnheadings); print $FILE4 "$headings\n"; my %file2; while (my $row = $csv->getline($FILE2)) { # chomp $row; my @fields = @$row; my $id = $fields[0]; $file2{$id}=["",@fields]; #print "$id\n"; } my %file3; while (my $row = $csv->getline($FILE3)) { # chomp $row; my @fields = @$row; my $id = $fields[0]; $file3{$id}=["",@fields]; #print "$id\n"; } while (my $row=$csv->getline($FILE1)) { my @fields=@$row; my $id=$fields[0]; if (exists $file2{$id}) { if(exists $file3{$id}) { my $fields_ref=\@fields; my @fields2=$file2{$id}[5]; my @fields3=$file3{$id}[5]; my @printline=$fields_ref."\t".\@fields2."\t".\@fields3; $csv->print ($FILE4,\@printline); } else { } } else { } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Expected fields to be an array ref
by armstd (Friar) on Oct 11, 2011 at 04:10 UTC |