How can I output a 2 dimensional array to a csv file in perl?
For example I have constructed a dimensional array as:
for my $i(0..100) {
for my $j (0..50) {
$data[$i][$j]=0;
}
}
And I want to output it to a csv file for future reference. And this csv file should be read and reconstructed to the 2 dimensional array. using code like:
my $outfilename="output.csv";
my $csv = Text::CSV->new;
open OUT , '<', $outfilename or die "Could not open $outfilename: $!";
while( my $row = $csv->getline( OUT ) ) {
push @output, $row;
}
close OUT;
What is an efficient way to do this?
for my $row (@data) { $csv->print(*OUT, $row); }to print the csv file? If that is how you intend to read it, it would make sense that it be the way you write it as well.