use text::CSV_XS; my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1, allow_whitespace => 1 }); open my $fh, "<", "final2.csv" or die "final2.csv: $!"; $csv->column_names ($csv->getline ($fh)); # Read the header line while (my $r = $csv->getline_hr ($fh)) { $File1Map{$r->{username}} = $r->{date_modified}; } : : $csv->eol ("\n"); # Now use it for output # inside the for/while loop $csv->print ($fh, [ $usename, $oldvalue, $newvalue ]);