Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

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


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

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

Replies are listed 'Best First'.
Re^4: line ending problem Text::CSV alternative Text::ParseWords?
by GertMT (Hermit) on Oct 06, 2009 at 12:09 UTC

    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

        this only changes....

        I mean that when I open data.csv and then hit the Save button and close the file and run the script everything works fine and I see the expected output.

        Tried you second suggestion and this kind of works! The expected output is visible in <>.

        Update

        The information I see on the screen looks structured. However a more structured way would be even better. I'm having a hard time finding out where to put my print "$id\t$brand\t$color\n"; statement as to have it print this for all records and not only the first line. Thought when I would do it before the closing bracket it would be in the loop? However I must me overlooking something.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-20 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found