dear Monks,

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 = &quotewords( ',', 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"; } }

In reply to parsing csv with Text::ParseWords by GertMT

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.