I am just trying to read .csv file first time.I have gone through the below link :
http://metacpan.org/pod/Text::CSV_XS#Reading-a-CSV-file-line-by-line:
I have few doubt, well if you want, u can tell me this are silly question but i don’t know, why i am not able to figure it out that how exactly perl is reading csv file 🙁
So, my doubt is:
First Question
What is the difference between reading the csv file line by line and parsing the file.
I have simple program where i am reading the csv file line by line.
Below is my program:
#!/usr/bin/perl -w
use strict;
use Text::CSV;
use Data::Dumper;
my $csv=Text::CSV->new( );
my $my_file="test.csv";
open(my $fl,"<",$my_file) or die"can not open the file $!";
#print "$ref_list\n";
while(my $ref_list=$csv->getline($fl))
{
print "$ref_list->[0]\n";
}
Below is the data in csv file :
"Emp_id","Emp_name","Location","Company"
102713,"raj","Banglore","abc"
403891,"Rakesh","Pune","Infy"
530201,"Kiran","Hyd","TCS"
503110,"raj","Noida","HCL"
Second Question:
If I want to get specific Emp_id along with Location then how can i proceed.
Third Question :
If I want only 102713 ,530201,503110 Emp record i.e name,location,compnay name then what should i do ?
Thanks
A CSV file is a good representation of tabular data in a text format, but it is unsuitable for an in-memory represenation. Because of that, we have to create an adequate representation. One such representation would be a hash:
If the header row is in the array
@header, we can create this hash with:We can then create lookup hashes that use the id values as keys. So
%lookuplooks like this:We populate it by doing
after the above loop. We can then access a certain hashref with
or access certain elements directly with
If the key does not exist, these lookups should return
undef.If the CSV file is too big, this might cause perl to run out of memory. In that case, the hashes should be
tied to files, but that is a different topic completely.