Newbie
Newbie

Reputation: 2728

Reading a csv and creating a new csv using perl

I am trying to read a csv , select some columns from the csv abc.csv and create a new csv def.csv . Here is my code below(snippet):-

genNewCsv();

   sub genNewCsv {
    my $csv = Text::CSV->new();

    my $aCsv = "abc.csv"
    my $mCsv = "def.csv";

    my $fh = FileHandle->new( $aCsv, "r" );

    my $nf = FileHandle->new($mCsv,"w");

    $csv->print($nf,[ "id","flops""ub" ]);
    while (my $row = $csv->getline($fh)){

        my $id = $row->[0];

        my $flops = $row->[2];

        my $ub = "TRUE";
        $csv->print($nf,[ $id,$flops,$ub]);
    }
    $nf->close();
    $fh->close();
}

When I do run this code , in def.csv file I get lines without a break in the line i.e id,flop,ub,ASM_1,in,TRUE,ASM_2,out,TRUE I want the csv to be one line after the other i.e.

id,flop,ub

ASM_1,in,TRUE

ASM_2,out,TRUE

Can anyone please help me .

Upvotes: 1

Views: 112

Answers (1)

Newbie
Newbie

Reputation: 2728

BINGO

I should be using this

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

Upvotes: 3

Related Questions