in reply to CSV Columns: Friend or foe?
I would suggest a different data structure. You are using a hash of arrays. I would suggest an array of hashes, one for each row. Many times, a parallel set of arrays (HoA) can be better handled as an array of hashes (AoH) with map to pull the requested column.
You can assign the rows using a hash slice or a for loop. You show that you know how to use the loop, so here is a hash slice:
var %record, @records; @record{@colNames} = split(',', $line); push @records, \%record;
To extract data from this structure:
sub total_inventory { my $data = shift; List::Util::sum(map {$_->{'Inventory'}} @$data); }
The data is kept together (shuffling one column will not break the relationships with the rest of the columns).
If you use DBI with one of the CSV modules, you may even be able to use some of the function of an SQL sum() call. When (ok, if) your data moves into a database, you can then migrate with (hopefully) less effort.
--MidLifeXis
|
|---|