As with most common tasks in Perl, someone has already written an excellent (and fast!) CSV parser and made it available via the CPAN as Text::CSV_XS. Using that module, it's easy to build a generic solution that parses each row of your CSV file into a hash using names provided on the first row:
#!/usr/bin/env perl
use strict; use warnings;
use Text::CSV_XS;
use IO::File;
my $io = IO::File->new('/tmp/Allcontrol.csv','<') # open file
or die("Cannot open data source file: $!"); # or die trying
my $csv = Text::CSV_XS->new();
my $head_row = $csv->getline($io); # get first line (headers)
my @dataset;
while (my $row = $csv->getline($io)) {
my %row_hash = map { $headrow->[$_] => $row->[$_] } (0..$#{$headrow
+});
push @dataset,\%row_hash;
}
At this point, @dataset is an array of hashes, where each hash represents a row
in the database. From that general solution, you should be able to extrapolate your solution. For example, you can manipulate the data before pushing the row into the set, etc.
<–radiant.matrix–>
Ramblings and references
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet
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.
|
|