Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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

by GertMT (Hermit)
on Oct 06, 2009 at 11:52 UTC ( #799452=note: print w/replies, xml ) Need Help??

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

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...

Replies are listed 'Best First'.
Re^3: line ending problem Text::CSV alternative Text::ParseWords?
by ELISHEVA (Prior) on Oct 06, 2009 at 11:56 UTC

    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.

        When you say, "I see nothing ... this only changes...", what precisely do you mean? Do you mean you the message goes away and nothing at all prints out? Or do you mean that when the message goes away, you get something printed out, just not the data you expected. Perhaps you are seeing one or more "<>" printed out?

        What happens if you put a print statement in the second code sample, e.g.

        my $line; my @fields; while ( $line = <FILE> ) { print STDERR "<$line>\n"; @fields = &quotewords( ',', 0, $line ) or ( warn "a problem on line $.:$_" ); # rest of code ... }

        If the second loop also prints out nothing, then it would look like your file has no lines in it or your program is dying before it ever gets to the while loop. In rare circumstances, programs can die while doing "print STDERR", but this doesn't normally happen when you are only printing strings. Is this all of your code or do you have some extra material before the while loop?

        The one thing that isn't likely is a problem with line endings. If there were a problem finding the line endings you would get one long line. If there was even one line, even an empty line, the print statement would at least print <>. That is why we made sure there was some non-whitespace in your print statement.

        Are you absolutely sure that the file you are reading in is the file you think you are reading in? Your code is using relative paths. That means it will read the file in the current directory that has the name data.csv. Maybe this is an empty file with the same name as the real csv file? Have you tried running the program using a fully qualified path?

        Best, beth

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2021-10-23 21:33 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (88 votes). Check out past polls.