I am parsing a CSV file and I would like to turn the columns into individual arrays contained in a hash which I can access by name (names are contained in the header line of the input file).
Example input file (MyCSV.csv):
Name,Cost,Inventory
pickles,2.99,12
vegemite,4.00,8
nuclear sub,22.50,4
What I have done thus far:
open IN, "MyCSV.csv" or die "Cannot open input file: $!\n";
my ($line, @colNames, $size, %columns, $j);
$j = 0;
chomp($line =<IN>);
# Read the column header
@colNames = split(',',$line);
$size = scalar @colNames;
while($line = <IN>)
{
chomp($line);
push @{$columns{$colNames[$j++ % $size]}}, $_ for(split(',', $line
+));
}
# Now I can play with whole columns of data by name
print “Total Inventory = ”, List::Util::sum(@{$columns{'Inventory'}}),
+ “\n”;
print “Total Cost = ” , List::Util::sum(@{$columns{'Cost'}}), “\n”;
A couple of quick notes:
- I know I should be using Text::CSV or Text::CSV_XS, but this is just for testing
- I am fairly new to Perl, but I really like it and I think there is a
better more Perl-tastic way of doing this
- I tried preallocating the arrays in the hash (using $#@{$columns{$colName}} = 100, where $colName came from iterating over @colNames), but I ran into problems where push started adding at position 101 and leaving the preallocated slots empty
I really like this approach because I don't have to create $size arrays, I just need to know what the columns are called (which is fairly standardized for my data files).
I thought there might be a way of transposing the rows/columns after reading the file completely into memory then I could just store Text::CSV(_XS)->fields() as my columns of data, but I didn't see such a facility in Text::CSV(_XS).
The runtime on this isn't horrible, but it is still linear in the number of columns (though not so linear in the number of rows). I have done time trials on 16 to 128 columns with 10,000 rows and the runtime is between 0.2 and 0.9 seconds, respectively, but I feel it could be better. Any thoughts are greatly appreciated.
Thanks.
- Tim
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.