in reply to Why is my data structure wrong?
use strict; use Text::CSV; use Data::Dumper; my (%report,@headers); my $csv = Text::CSV->new(); while(<DATA>) { die unless $csv->parse($_); if ($. == 1) { @headers = $csv->fields(); $report{headers} = \@headers; } else { my @row = $csv->fields(); push @{$report{data}}, {map{$headers[$_]=>$row[$_]} (0..$#row)}; } } print Dumper \%report; __DATA__ foo,bar 333,aaa 444,bbb 555,ccc
Update:
"This fails because, as I mentioned, I don't have direct access to the CSV data and thus cannot use Text::CSV"
I probably chose the words 'unit test' poorly. When i find myself in this situation i try to back away and try another route. I only used Text::CSV to abstract that portion of the problem, as the data structure was what was giving you the trouble.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (jeffa) Re: Why is my data structure wrong?
by Ovid (Cardinal) on Jun 24, 2002 at 21:09 UTC |