Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: line ending problem Text::CSV alternative Text::ParseWords?

by ELISHEVA (Prior)
on Oct 06, 2009 at 11:28 UTC ( [id://799441]=note: print w/replies, xml ) Need Help??


in reply to line ending problem Text::CSV alternative Text::ParseWords?

What happened with your first code sample? Did it also stop on the first line?

I see you are only using strict, not warnings. Diagnostics does not turn on warnings automatically. When you add use warnings, what do you see? Do you get any warnings? What diagnostics print out with them?

If you really have a problem with line endings it should be obvious in your loops. In the first bit of code, what happens if you include the line local $"='|'; print STDERR "<@$row>\n" at the top of the while loop in the first example? If you include print STDERR "<$line>\n"., what happens?

In the two print statements, you'll note that I sent output to STDERR. This is to make the debugging statements easier to find when you are ready to comment them out or delete them. I also surrounded the variable I wanted to print with "<...>" and changed the separator between array elements in a string to a pipe ('|'). This is done so that the presence of whitespace is more obvious.

Best, beth

Replies are listed 'Best First'.
Re^2: line ending problem Text::CSV alternative Text::ParseWords?
by GertMT (Hermit) on Oct 06, 2009 at 11:52 UTC
    Thanks for your reply,
    thought that the -w would trigger warnings as well. Anyhow I did put use warnings; in my code but no warnings.
    guess I'm losing my way a bit.. should it look like this?
    my $row; local $"='|'; print STDERR "<@$row>\n"; while ( my $row = $csv_in->getline($fh_in) ) { $csv_out->print( $fh_out, [ @$row[ 2, 3, 0, 4 ], ] ); } print STDERR "<$row>\n";
    I get
    Can't use an undefined value as an ARRAY reference at but I'm not sure I put the code in the correct way. Trying to figure this out...

      Like this:

      while ( my $row = $csv_in->getline($fh_in) ) { # print out each line you visit during the while loop local $"='|'; print STDERR "<@$row>\n"; $csv_out->print( $fh_out, [ @$row[ 2, 3, 0, 4 ], ] ); }

      Your code was getting Can't use an undefined value as an ARRAY reference at because you were trying to dereference $row as an array reference before you ever used it as an array reference. Also "my" in a while statement scopes the variable to inside the while loop. my $row outside the while loop and while (my $row...) are actually to different variables with two different locations in memory. Using the variable as an array reference inside the while loop doesn't have any effect on how Perl sees the variable of the same name found outside of the while loop.

      Best, beth

        aha, I see. Tried this and don't get anything back on the screen. Well except for:

        Program exited with code #0 after 0.12 seconds.

        This only changes if I open the file data.csv and save it again.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://799441]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-25 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found