As an aside, it is strongly recommended that you declare variables as close to the point they are used as possible. Generally that means at the place where the first assignment is made to them. Consider:

while (my $inBuf = <csvFILE>) { chomp($inBuf); $inBuf =~ s/^\"//; # Take out any LEADING or $inBuf =~ s/\"$//; # TRAILING double quotes # OK, Process this record my @fields = split(/\,/,$inBuf); print "\nProcessing record $fields[0]"; push @rows, \@fields; # Can push \@fields because @fields is local t +o the loop } # End of csvFILE or we reached our Runaway count close csvFILE;

Note that the duplicated counters have gone. scalar @rows gives you the line count in any case. If you need an item count then the correct code would be:

my $items = 0; while (...) { ... $items += @fields; # Array in scalar context gives number of elemen +ts }

I presume you use strictures (use strict; use warnings;. If not you ought, always!

Also, parsing csv can be tricky. What will happen if your code meets the following line for example?

1,"This, or this and that",3

The better solution is to use one of the modules designed for the purpose such as Text::CSV.


DWIM is Perl's answer to Gödel

In reply to Re: Using Array of Arrays by GrandFather
in thread Using Array of Arrays by Aim9b

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.