in reply to extracting data from CSV files and making hash of hashes
use strict; use warnings; use Data::Dumper; use Text::CSV; use File::Basename; my @files = glob '*.csv'; my %hMFD; foreach my $file (@files){ my $file_base = fileparse($file, qr/\.csv/); my $csv = Text::CSV->new({sep_char => ';'}); open (CSV, "<", $file) or die $!; while (<CSV>) { if ($csv->parse($_)) { my @columns = $csv->fields(); $hMFD{$file_base}{$columns[0]} = $columns[1]; } else { my $err = $csv->error_input; print "Failed to parse line: $err"; } } close CSV; } print Dumper(\%hMFD); __END__ $VAR1 = { '1' => { 'Table' => 'M', 'Data' => 'O' }, '2' => { 'Table' => 'X', 'Data' => 'Y' } };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: extracting data from CSV files and making hash of hashes
by reez (Novice) on Jul 21, 2010 at 11:07 UTC | |
by toolic (Bishop) on Jul 21, 2010 at 12:59 UTC |