Your immediate problem has been resolved. However there are a few other things you ought to consider:

unless (open(INFILE, $csv_filename)){ print "Cannot open file \"$csv_filename\"\n"; exit; }

is better written:

open my $inFile, '<', $csv_filename or die qq{Cannot open file "$csv_f +ilename"\n};

which declares a lexical file handle, uses the safer three parameter version of open and uses the more idiomatic die for error handling.

foreach $line (<INFILE>) {

slurps the file and introduces a variable that, in a subtle fashion, isn't the one you think it is (see Foreach Loops and note that the variable is aliased). You would be better to use a while loop:

while (defined (my $line = <$inFile>)) {

The close INFILE should be outside the loop. It didn't matter for the foreach loop because you were slurping the file, but it will cause trouble for the while loop version.

my @field = $csv->fields; my $count = 0; for my $column (@field) { ++$count; $d_array[$line_number][$count] = $column; }

is better written:

$d_array[$line_number] = [$csv->fields ()];

Update: oh, and if you use $. instead of $line_number you get the actual file line number. Note though that $d_array[0] at least will be undef using either your current line counting technique or the $. technique.


Perl reduces RSI - it saves typing

In reply to Re: dynamic 2d array by GrandFather
in thread dynamic 2d array by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.