Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-25 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found