my $csv_in = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 }); my $csv_out = Text::CSV_XS->new ({ binary => 1, auto_diag => 1, eol => "\n" }); open my $fh_old, "+<", $file or die Lava::Message ("Can't open original file: $file: $!"); open my $fh_new, ">", $temp or die Lava::Message ("Can't open tempfile file: $temp: $!"); $csv->getline ($fh_old); # skip first line. Header?. If empty line, 'scalar <$fh_old>;' is a good option while (my $row = $csv->getline ($fh_old)) { : : $csv_out->print ($fh_new, [ map { $row->[$_] } 0, 2, 1, 3, 0 ]); } $csv_out->print ($fh_new, [ 0, 0.000, 0.000, 0.000 ]); $csv_out->print ($fh_new, [ 0, 0.000, 0.000, 0.000, "END" ]); close $_ for $fh_old, $fh_new;