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

Re: Writing CSV files

by TilRMan (Friar)
on Oct 29, 2004 at 15:07 UTC ( #403781=note: print w/replies, xml ) Need Help??

in reply to Writing CSV files

Here's a quick subclass of Text::CSV_XS to return you an opaque $quote object from getline() that you pass to print(). Two gotchas though: First, commas embedded in a field screw it up. Second, I only implemented new, getline(), and print(); not combine() or parse() or anything else.
#!/usr/bin/perl use strict; use warnings; { package Text::CSVPreserveQuotes; use base 'Text::CSV_XS'; sub new { my ($class) = @_; my $self = $class->SUPER::new ({ quote_char => undef, eol => "\n", escape_char => undef, sep_char => ",", }); bless $self, $class; } # my ($row, $quote) = $csv->getline($io); # # $quote is opaque; pass it to print() # sub getline { my ($self, $io) = @_; my $row = $self->SUPER::getline($io); return if not defined $row; my $quote = []; for (@$row) { push @$quote, s[^"(.*)"$][$1]s ? 1 : 0; } return ($row, $quote); } # $csv->print($io, $row, $quote); # # $quote is what you got from ->getline() # # $row and $quote will get mangled! # sub print { my ($self, $io, $row, $quote) = @_; for (@$row) { $_ = qq{"$_"} if shift @$quote; } $self->SUPER::print($io, $row); } } use IO::Wrap qw( wraphandle ); my $data = wraphandle(\*DATA); my $stdout = wraphandle(\*STDOUT); my $csv = Text::CSVPreserveQuotes->new(); while (1) { my ($row, $quote) = $csv->getline($data); die if not defined $row; last if @$row == 0; # Munge # for (@$row) { if (/^[\d.]+$/) { $_ += 42 } else { tr/A-Za-zm/N-ZA-Mn-za-m/ } $_ = "<$_>"; } $csv->print($stdout, $row, $quote); } __DATA__ "hi",3,20.6,"green","32" 16,"alpha",0.00 "bye",3,27.6,"green","32" 16,"beta",0.00 "This won't work, since it has an embedded comma"

If the strings can have commas, I don't know if Text::CSV_XS can help you.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://403781]
[Corion]: I have to write at least one not so nice mail, give a stern word to some colleagues who took changes into production without getting approval and actually get some work done...
[choroba]: too busy to work on my mood :-(
choroba having "blue pipeline duty" this week
[Corion]: choroba: Exactly that, but I prefer to keep an eye on my mood nowadays...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2017-03-29 07:33 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (344 votes). Check out past polls.