1

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 .

1 Answer 1

3

BINGO

I should be using this

 my $csv = Text::CSV->new ({ binary => 1, eol => $/ });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.