I will only comment on the flaws in script 1, as that deals with Text::CSV
- my @names = $csv->getline ($io); assigns an array ref to a list. That's not what you want.
my @names = @{$csv->getline ($io)}; is the coreect mantra.
- always_quote is void on parsing. That is a generator option.
- make it a habit to always pass binary => 1
- Use auto_diag => 1 on csv initiation or $csv->oef or $csv->error_diag () after parsing
use strict;
use warnings;
use Text::CSV;
use Data::Dumper;
open my $io, "<", "player_characters.csv" or die "player_characters.cs
+v: $!";
my $csv = Text::CSV_XS->new ({
sep_char => "|",
allow_whitespace => 1,
blank_is_undef => 1,
binary => 1,
auto_diag => 1,
});
my @names = @{$csv->getline ($io)};
$csv->column_names (@names);
while (defined (my $hr = $csv->getline_hr ($io))) {
print Dumper ($hr);
}
If you do not use the @names after you set it, other than to assign to to column names, you can combine the in one call, as column_names () accepts both a list or a list ref
$csv->column_names ($csv->getline ($io));
Enjoy, Have FUN! H.Merijn
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.