trey5498 has asked for the wisdom of the Perl Monks concerning the following question:
I am currently trying to learn Perl (never done until now) and I wrote a small peice of code that I want to read in from an CSV file and place it into an array. At this time I want to print each line of the array so that I know that I am doing it right. Later I will putting it into an output.csv with some changes made.
Here is the code which doesnt seem to working right. PLEASE HELP!!!!!!!!!!!!
Code
use Text::CSV;
my @printerstat;
$file = 'inputdata.csv';
my $csv = Text::CSV->new();
open (FILE, $file) or die "Couldn't open location file: $!";
while (<FILE>) {
$csv->parse($_);
push(@printerstat, [$csv->fields]);
print @printerstat;
}
close FILE;
Re: Need help with arrays and CSVs
by ww (Archbishop) on Dec 26, 2007 at 20:35 UTC
|
OP's code, for ease of reading, with a note re the missing "my" and strict and warnings added:
#!C:/perl/bin
use warnings;
use strict;
use Text::CSV;
my @printerstat;
$file = 'inputdata.csv'; # should be <b>my</b> $file
my $csv = Text::CSV->new();
open (FILE, $file) or die "Couldn't open location file: $!";
while (<FILE>) {
$csv->parse($_);
push(@printerstat, $csv->fields);
print @printerstat;
}
close FILE;
when run against a csv:
this is a field to start with,field2,field3
this produces what I would expect, namely:
this is a field to start with.field2field3
On the supposition that "I want to print each line of the array" means you want each field in the csv printed on a separate line replace
print @printerstat;
with:
for my $field(@printerstat) {
print "$field\n";
}
| [reply] [d/l] [select] |
|
while (<FILE>) {
$csv->parse($_);
push(@printerstat, $csv->fields);
# print @printerstat;
}
spit(@printerstat);
sub spit {
for my $field(@printerstat) {
print "$field\n";
}
}
thusly
this is a field to start with
field2
field3
#
*
field3 of line 2
field1 of line3
and this is the second field of line3
field3 of line3
This also works if some of your fields are void, eg:
,,this is a row with two empty fields
| [reply] [d/l] [select] |
Re: Need help with arrays and CSVs
by jZed (Prior) on Dec 26, 2007 at 20:33 UTC
|
From the docs for Text::CSV_XS which also apply to Text::CSV:
my $csv = Text::CSV->new ({ binary => 1, eol => $/ });
open my $io, "<", $file or die "$file: $!";
while (my $row = $csv->getline ($io)) {
my @fields = @$row;
}
P.S. Please use <code>...</code> tags so your code is more legible. | [reply] [d/l] [select] |
Re: Need help with arrays and CSVs
by derby (Abbot) on Dec 26, 2007 at 20:13 UTC
|
Here is the code which doesnt seem to working right.
Well ... what's not working right?
PS ... welcome to perl and the monastery ... check out How (Not) To Ask A Question.
| [reply] |
Re: Need help with arrays and CSVs
by NetWallah (Canon) on Dec 26, 2007 at 20:27 UTC
|
Please use code tags described in Writeup Formatting Tips.
Your code puts all the parsed columns in the entire file into a single flat array, printing the array as it accumulates.
Do you need to accumulate the entire file before processing it, or will you be working one line at a time ?
If you let us know what you want the code to accomplish, and what error messages you are getting, perhaps we can assist you better.
"As you get older three things happen. The first is your memory goes, and I can't remember the other two... "
- Sir Norman Wisdom
| [reply] |
Re: Need help with arrays and CSVs
by jrsimmon (Hermit) on Dec 26, 2007 at 21:00 UTC
|
Hello trey5498-
Ok, so I cleaned this up a bit and added code tags. After I installed Text::CSV, this code ran perfectly well on my machine. It basically takes every comma separated value and pushes it onto the @printerstat array. Is that what you intended? If not, you'll need to be more specific about the goal your attempting to reach and the problem you're encountering.
Please note the use of use strict;
use warnings;
at the beginning of the script. I've been casually coding in perl for a few years now and have found them to be invaluable.
Also see PerlMonks FAQ, Writeup Formatting Tips, and How do I post a question effectively? to familiarize yourself with the surroundings.
use strict;
use warnings;
use Text::CSV;
my @printerstat = ();
my $file = 'inputdata.csv';
my $csv = Text::CSV->new();
open (FILE, $file) or die "Couldn't open location file: $!";
while (<FILE>) {
$csv->parse($_);
push(@printerstat, $csv->fields);
print @printerstat;
}
close FILE;
| [reply] [d/l] [select] |
|
|