Perl parsing the csv file

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

Answers


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:

my $hashref = {
   Emp_Id   => ...,
   Emp_name => ...,
   Location => ...,
   Company  => ...,
};

If the header row is in the array @header, we can create this hash with:

my @header = ...;
my @row = @{$csv->getline($fl)}; # turn the arrayref into an array
my $hashref = {};
for my $i (0..$#header) {
  $hashref->{$header[$i]} = $row[$i];
}
# The $hashref now looks as described above

We can then create lookup hashes that use the id values as keys. So %lookup looks like this:

my %lookup = (
   102713 => $hashref_to_first_line,
   ...,
);

We populate it by doing

$lookup{$row[0]} = $hashref;

after the above loop. We can then access a certain hashref with

my $a_certain_id_hashref = $lookup{102713};

or access certain elements directly with

my $a_certain_id_location = $lookup{102713}{Location};

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.


Need Your Help

How to fill web forms automatically in AutoIt script using Firefox instead of IE?

firefox autoit

I’ve been away from Autoit for a while and I’m looking to rewrite my scripts to use Firefox instead of IE.

Bean Managed Transaction in a Container Managed EJB

ejb-3.0 java-ee-6

I have a stateless EJB that using container managed transactions. Can I have a method in that EJB that can use bean managed transaction. I know I can make the whole EJB to use the bean managed

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.