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.
In reply to Re: Using Array of Arrays
by GrandFather
in thread Using Array of Arrays
by Aim9b
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |