splice is the super-tool in Perl to extract elements from an array or to replace some elements.

You cannot apply it directly to CSV file but we know we can use the Text::CSV module to read and write arbitrary CSV files. We can use that to read in a CSV file line-by-line and as each line is represented as an array we can use splice on the array.

Effectively working on the columns of the CSV file.

Given this CSV file



we would like to create another file that features the 1st, 2nd, and 4th column.

Here is our solution:


use strict;
use warnings;
use Text::CSV;

my $csv = Text::CSV->new( { binary => 1 } );

open my $in,  '<:encoding(utf8)', 'examples/data/multiline.csv' or die;
open my $out, '>:encoding(utf8)', 'examples/data/multiline_spliced.csv' or die;

while ( my $row = $csv->getline( $in ) ) {
    splice @$row, 2, 1;

    my $status = $csv->combine(@$row);
    if ($status) {
       print $out $csv->string(), "\n";

close $in;
close $out;

At first we create an instance of Text::CSV, the de-facto standard CSV-parser of Perl.

We open both the original file for reading, and the output file for writing.

Then we use the getline method of the CSV parser to read the logical lines of the CSV file. We need to do this instead of reading ourselves, because some fiels in the CSV file might contain embeddd newline, just as in our example we have "Hofeherke,\nalma".

For each logical row, getline will return a reference to an array that holds the fields of the current line. Dereferencing it by writing @$row allows us to use splice on the array.

Then we use the combine method of the CSV parser that puts together the fields and adds the quotes where necessary. It returns a boolean value indicating the success or failur of combining the fields. (Though I am not sure what can ogo wrong here.)

The string method returns the actual CSV string representing the current line. We can print it to the output file followed by a new-line.

The resulting file is this:



Other CSV related articles

CSV in Perl