GertMT has asked for the wisdom of the Perl Monks concerning the following question:
While reading a csv-file I've had some trouble using Text::CSV_XS for a reason that might have to do with eol (re. my post from y'day where Tux and ELISHEVA give me valuable info). That's why I'm trying to use Text::ParseWords but I just can't get to the final stage and was wondering if someone could help me.
I know that there's a lot of 'fields' as I counted them. While trying to print them in an orderly way I get all of them (not orderly and commented out) or just the value of the elements from the first line.
The last line of code obviously doesn't make sense. The fields should be coming from 'every single line', (@lines? and not @fields?)
Needless to say I'm not able to use the defined variables.What am I doing wrong?
I did check some docs on refences and so but all is a bit confusing now.
thanks, Gert#!/usr/bin/perl use warnings; use strict; use diagnostics; use Text::ParseWords; my $csvfilename = "data.csv"; open( FILE, "<$csvfilename" ) or die("Couldn't open CSV file $csvfilename:$!\\n"); # Read lines from STDIN. my $line = (); my @fields = (); my $field = (); while ( $line = <FILE> ) { @fields = "ewords( ',', 0, $line ) or ( warn "problem on line $.:$_" ); # Set variable values based on the array values. my $id = $fields[0]; my $brand = $fields[1]; my $dbt = $fields[2]; my $cdt = $fields[3]; my $color = $fields[4]; my $number = $fields[5]; # print "@fields\n"; my $arraySize = $#fields + 1; print "array size = $arraySize\n"; print "array size = ", @fields . "\n"; print __LINE__ + 1 . ": I'm in trouble here...\n"; foreach $line (@fields) { print "$fields[0]\t$fields[1]\n"; } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: parsing csv with Text::ParseWords
by Marshall (Canon) on Oct 08, 2009 at 22:06 UTC | |
by GertMT (Hermit) on Oct 09, 2009 at 11:31 UTC | |
Re: parsing csv with Text::ParseWords
by Bloodnok (Vicar) on Oct 09, 2009 at 01:05 UTC | |
by GertMT (Hermit) on Oct 09, 2009 at 11:34 UTC | |
by Marshall (Canon) on Oct 12, 2009 at 00:54 UTC | |
by GertMT (Hermit) on Oct 12, 2009 at 11:44 UTC | |
by afoken (Chancellor) on Oct 13, 2009 at 18:56 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |