bones07 has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use Text::CSV; $file = 'testfile.csv'; my $csv = Text::CSV->new(); #Bring in our csv, parse it, and make a hash of filenames (keys) and I +D numbers (values) open (CSV, "<", $file) or die $!; my @content; %masterlist; while (<CSV>){ next if ($. == 1); if ($csv->parse($_)){ my @columns = $csv->fields(); $masterlist{"$columns[0]"} = "$columns[11]"; }else{ my $err = $csv->error_input; print "Failed to parse line: $err"; } } close CSV; # Invert the array twice to get rid of any duplicates that may have go +tten into our hash %masterlist = reverse %masterlist; %masterlist = reverse %masterlist; #Show us the output to make sure the keys and values are lined up corr +ectly foreach $key ( keys %masterlist ) { print $key, " => ", $masterlist{$key}, "\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Please help with CSV
by GrandFather (Saint) on Apr 14, 2011 at 23:55 UTC | |
|
Re: Please help with CSV
by Tux (Canon) on Apr 15, 2011 at 05:53 UTC |