Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: Hash & CSV
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 10755 of 11982
Post > Topic >>

Re: Hash & CSV

by prabu.ayyappan@[EMAIL PROTECTED] (Prabu Ayyappan) Mar 18, 2008 at 04:03 AM

--0-597986173-1205838222=:24685
Content-Type: text/plain; charset=us-ascii

----- Original Message ----
From: JBallinger <ballingerjohns@[EMAIL PROTECTED]
>
To: beginners@[EMAIL PROTECTED]
 Monday, March 17, 2008 9:18:12 PM
Subject: Re: Hash & CSV

On Mar 14, 3:26 pm, manojkum...@[EMAIL PROTECTED]
 (Manoj) wrote:
> When using Data: Dumper is taking more time for my 10000 lines of CSV
file.
> This solved a few queries...and the benchmark was a new value addition
for
> me. Thanks
> > 2)       Is there any optimal method for reading a CSV file and put to
> hash table.
>
> > May have better approaches however this read CSV into Hash
>
> > use Data::Dumper;
> > open(INFILE, "<", "sample.csv") or die $!;
> > my %hsh;
> > %hsh = ( %hsh, (split(/,/, $_))[1,2] ) while ( <INFILE> );
>
> That is a *very* inefficient way to populate a hash as you are copying
> the entire hash for every record in the file.  Better to add the keys
> and values individually:
>
> my %hsh;
> while ( <INFILE> ) {
>      chomp;
>      my ( $key, $value ) = split /,/;
>      }
>
> > print Dumper \%hsh;
>
> John

> - Show quoted text -

Suppose my csv file has 5 columns: f id fa mo ge
Will my ($key, $value) = split/,/; still work?
Is there any other mothod that is more efficient?

JB


Hi,
 
There is a module use AnyData;
which we can use for different format of files for CSV , here is a sample
piece of code
 
use AnyData;
use Data::Dumper;
$format= 'CSV';
$data = 'sample.csv';
$open_mode = 'r';
# Open file in read/update mode
my $table = adTie( $format, $data, $open_mode,);
print Dumper %$table;
 
Hope this helps.
 
Best Regards,
Prabu


     
____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now. 
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ


--0-597986173-1205838222=:24685--
 




 1 Posts in Topic:
Re: Hash & CSV
prabu.ayyappan@[EMAIL PRO  2008-03-18 04:03:42 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 1:44:33 CDT 2008.