Text::CSV is out of the question, too? I tried to do some "quick n dirty" CSV manipulation much like this recently but as usual things quickly got much more dirty than quick, I said bleep this, I'll use CPAN, and things were peachy. The main part of the resulting script looks like this:
my $csv = Text::CSV->new({
binary => 1,
sep_char => "\t",
always_quote => 1,
}) or do {
print STDERR "Cannot initialize CSV: ", Text::CSV->error_diag, "\n
+";
exit 1;
};
LINE: while (my $row = $csv->getline(\*STDIN)) {
s/\n/$replace/g foreach(@$row);
unless($csv->combine(@$row)) {
print STDERR "Error converting record $. for output: ", $csv->
+error_input, "\n";
next LINE;
}
print $csv->string, "\n";
}
$csv->eof or $csv->error_diag;
I tried getting your code to work but couldn't, maybe for lack of CRs in my source, but I'd doubt it covers all the subtleties CSV allows regarding quoting and escaping, let alone those it doesn't allow but you'll find anyway.
Edit: I think we got ourselves a consensus here :-D
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.