Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Read data from csv file to a data structure format

by Tux (Monsignor)
on May 18, 2012 at 07:17 UTC ( #971229=note: print w/ replies, xml ) Need Help??


in reply to Read data from csv file to a data structure format

Like this?

use v5.14; use warnings; use autodie; use Text::CSV_XS; use Data::Peek; my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 }); my $hdr = $csv->getline (*DATA); $csv->column_names ($hdr); my ($pn, $pc, @comp) = @$hdr; my %comp = map { $_ => {} } @comp; while (my $row = $csv->getline_hr (*DATA)) { my $p = $row->{$pc}; $comp{$_}{$p} = $row->{$_} for @comp; } DDumper \%comp; __END__ Product Name,Product Code,Company1,Company2,Company3 Test One,C001,Y,N,N Test Two,C002,N,N,Y Test Three,C003,Y,Y,N => { Company1 => { C001 => 'Y', C002 => 'N', C003 => 'Y' }, Company2 => { C001 => 'N', C002 => 'N', C003 => 'Y' }, Company3 => { C001 => 'N', C002 => 'Y', C003 => 'N' } }

Enjoy, Have FUN! H.Merijn


Comment on Re: Read data from csv file to a data structure format
Download Code
Re^2: Read data from csv file to a data structure format
by Anonymous Monk on May 18, 2012 at 07:56 UTC
    Thanks a lot for your help.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-08-30 19:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls