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 (Abbot)
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

Replies are listed 'Best First'.
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?
[shmem]: Discipulus: while (readdir DIR) works fine
[Discipulus]: Eataly vs World(coffe) ;=)
[Discipulus]: shemem i wrote it directly.. but when i tested it i got a lot of 'undefined value..'
[Corion]: Oh, poor Trump can't get a break. Even the Vatican only sends the second in command to greet him...
[shmem]: Discipulus: readdir has the same semantics as readline (a.k.a <>). Have another coffee ☕ ;-)
[Discipulus]: ah! just with my old system perl (5.8.8) was needed
[Corion]: Has anybody toyed around with Mastodon? There is a Perl module for it, but I'm not sure that it's a yak worth chasing
[Corion]: On the upside, maybe running that for my family would be interesting, or at least for myself
[Corion]: I'm not fond of using a server hosted by other people, even if they claim to be GNU ;)

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2017-05-26 07:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?