Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Parse error

by naChoZ (Curate)
on Dec 18, 2007 at 14:34 UTC ( #657681=note: print w/ replies, xml ) Need Help??


in reply to Parse error

I'm sure Tie::CSV_file is a fine module, but you might also give Parse::CSV a spin. Here's some cutting and pasting of some code I wrote recently.

use Parse::CSV; # Define the names of our csv fields # my $csv_fields = [ 'CoordinatorID', 'CompanyName', 'FirstName', 'LastName', 'City', 'State', 'csgaccount', 'Username', 'DomainName' ]; # Populate these hashrefs with data for comparison with the # list of names. # my $provisioned = fetch_csv_accounts({ filename => $opts->{prov}, fields => $csv_fields }); # {{{ sub fetch_csv_accounts # sub fetch_csv_accounts { my $args = shift; die "No field names provided...\n" unless defined $args->{fields}; die "No filename provided...\n" unless defined $args->{filename +}; # Default the keyfield to Username if nothing # is passed to this sub. # my $keyfield = $args->{keyfield} ? $args->{keyfield} : 'Username'; # # Create a Parse::CSV object for easy csv iteration # my $prov_csv = Parse::CSV->new( file => $args->{filename}, fields => $args->{fields}, ); die "Error: " . $prov_csv->errstr . "\n" if $prov_csv->errstr; # Loop through each row of the csv file and populate the # hashref. The resulting hashref will look like: # # Username1 -> CoordinatorID # -> CompanyName # -> FirstName # -> Lastname # -> City # -> State # -> csgaccount # -> Username # -> DomainName # Username2 -> CoordinatorID # -> CompanyName # ... etc ... my $return_hash = {}; while ( my $row = $prov_csv->fetch ) { foreach ( keys %{$row} ) { $return_hash->{ $row->{$keyfield} }->{$_} = $row->{$_}; } } return $return_hash; } # }}}

--
naChoZ

Therapy is expensive. Popping bubble wrap is cheap. You choose.


Comment on Re: Parse error
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://657681]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2015-07-02 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (46 votes), past polls