Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Getting columns from csv file in perl

by pnsreee (Initiate)
on Apr 24, 2007 at 13:27 UTC ( #611727=perlquestion: print w/replies, xml ) Need Help??
pnsreee has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, <p. How can i get columns from cvs file into an array. The CSV file is a dynamic file.

the fields are like this

Numb,Name,Class,Type,...... 201,Bazaar,b2,53,.......... 121,Raymond,y1,02,... 232,Robert,p1,04,...............

I am using following code to get columns.

@tmp = `more +2 flat1.txt | cut -d "," -f $i` ;

It is giving out put and also error saying that refer help. Can any one give me equivalent to this

Edit: g0n - formatting tags

Replies are listed 'Best First'.
Re: Getting columns from cvs file in perl
by Fletch (Chancellor) on Apr 24, 2007 at 13:37 UTC

      You should also check the returned status when you parse a line:

      my $csv = Text::CSV_XS->new( { binary => 1 } ); my $status = $csv->parse($line); # parse a CSV string into fie +lds

      Occasionally, you'll encounter an invalid CSV line and you'll definitely want to handle it. If there are any embedded binary chars, you'll want to set the binary flag when you create the $csv object as I did above.

      Where do you want *them* to go today?
Re: Getting columns from cvs file in perl
by Joost (Canon) on Apr 24, 2007 at 13:38 UTC
      Hi Joost, It is csv file. I want expresion with out using Text::xSV or Text::CSV_XS.

        Just don't come back whinging when you have data with embedded commas or newlines and the quick hack no longer works. You may get away with it this time, but n months down the road someone (possibly even yourself) will be swearing under their breath at you as they clean up the mess.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://611727]
Approved by Joost
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2018-05-26 22:43 GMT
Find Nodes?
    Voting Booth?