in reply to Using Array of Arrays
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Using Array of Arrays
by Aim9b (Monk) on Sep 20, 2007 at 13:18 UTC | |
by GrandFather (Saint) on Sep 20, 2007 at 19:16 UTC | |
by Aim9b (Monk) on Sep 21, 2007 at 11:19 UTC | |
by GrandFather (Saint) on Sep 21, 2007 at 21:30 UTC | |
by Aim9b (Monk) on Sep 24, 2007 at 15:21 UTC | |
by GrandFather (Saint) on Sep 21, 2007 at 21:43 UTC | |
by Aim9b (Monk) on Sep 24, 2007 at 15:06 UTC |