tags:

views:

176

answers:

3

I want to extract a particular fields from a csv file (830k records) and store into hash. Is there any fast and easy way to do in Perl with out using any external methods?

How can I achieve that?

+9  A: 

Use Text::CSV_XS. It's fast, moderately flexible, and extremely well-tested. The answer to many of these questions is something on CPAN. Why spend the time to make something not as good as what a lot of people have already perfected and tested?

If you don't want to use external modules, which is a silly objection, look at the code in Text::CSV_XS and do that. I'm constantly surprised that people think that even though they think they can't use a module they won't use a known and tested solution as example code for the same task.

brian d foy
Note: Not using modules may be an external requirement or limitation of the given environment that joe has to conform to.
Dercsár
Note: if you can write code, you can install modules. It's an easy thing to explain to business people that you can get it better, cheaper, faster that way. "But I want to build a house without any tools!"
brian d foy
We are already having so many modules to lot of jobs . So limitation is i cant use any external modules. But here in this case . i guess that would b fine to use. Thanks Brian
joe
A: 

See also this code fragment taken from The Perl Cookbook which is a great book in itself for Perl solutions to common problems

p.marino
I am sorry for having linked to an apparently pirated copy of the book: the author used to have the code parts online in the past, and I didn't doublecheck the source when I put the link in. Now I have found a better source for the same code.
p.marino
A: 

assuming normal csv (ie, no embedded commas), to get 2nd field for example

 $ perl -F"," -lane 'print $F[1];' file
ghostdog74