Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Problem when - use strict and -w

by ikegami (Patriarch)
on Jul 14, 2009 at 21:44 UTC ( [id://780085]=note: print w/replies, xml ) Need Help??


in reply to Problem when - use strict and -w

Why aren't you using Text::CSV_XS for output too?
#!/usr/bin/perl -w use strict; use Text::CSV_XS qw( ); my $qfn_in = 'data.txt'; open(my $fh_in, '<', $qfn_in) or die("Can't open file \"$qfn_in\": $!\n"); my $fh_out = \*STDOUT; my $csv_in = Text::CSV_XS->new({ sep_char => ',', eol => $/ }); my $csv_out = Text::CSV_XS->new({ sep_char => "\t", eol => $/ }); my $number = 10; while ( my $row = $csv_in->getline($fh_in) ) { $csv_out->print($fh_out, [ "Olah:", $number++, @$row[2,5,3,0,4], ]); }

Mind you, the output format is slightly different than yours. (It will quote all fields or only necessary fields depending on option always_quote).

It could use error checking, but you didn't have any either.

Replies are listed 'Best First'.
Re^2: Problem when - use strict and -w
by GertMT (Hermit) on Jul 15, 2009 at 03:22 UTC
    Like your proposed code of reading the data. The empty last line problem is now also not an issue anymore. Don't think I can have Text::CSV_XS handle the output as I need to work on the output such as: (psuedocode)
    multiply row->[2] by 4 if row->[0] != 123
    Though definitely something I'll consider using in the future.
    Thanks
      What part of that do you think you can't do with $csv->print (but can somehow do with STDOUT->print)
        ah well, I'm kind of puzzled in the setup $csv->print where to put the part of the code that's going to work on the columns. In my (new) setup I have:
        while ( my $row = $csv_in->getline($fh_in) ) { # doing stuff with the columns print "Olah:","\t", $number++, $row->[2]... }
        How should I structure this using $csv->print. Probably just a small hint/example will open my eyes (again).
        Gert

Log In?
Username:
Password:

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

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

    No recent polls found