Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Text::CSV

by wilmer_t (Novice)
on Aug 20, 2014 at 13:33 UTC ( #1098115=note: print w/replies, xml ) Need Help??


in reply to Text::CSV

It's true that Text::CSV does not directly handle multiline fields, but I found it quite easy to get around
My task was to export/import a set of requirements between tools, for which purpose I (amongst others) needed to add a field and sort the input recursively by parent reference (using Sort::Naturally)

On reading, I created multilines as

while ( my $line = $csv->getline($fh) ) { # Manipulation of unsorted fields during import my @multiline = join(';',@{$line}); push @content, @multiline ; } if (not $csv->eof) { $csv->error_diag(); }

and on printing back, I entered data field by field as
for my $line (@result) { my @fields = split(/;/,$line); # Manipulation of sorted fields during export print $fh "$_;" foreach @fields; print $fh "\n"; }
with csv object created as $csv = Text::CSV->new({ binary => 1, auto_diag => 1, sep_char => ';' });

I.e. I never bothered to try using the module for writing back data, nor other suggestions as from above :)

Replies are listed 'Best First'.
Re^2: Text::CSV
by Anonymous Monk on Aug 20, 2014 at 14:32 UTC
    It's true that Text::CSV does not directly handle multiline fields

    What is your definition of "multiline fields"? Because Text::CSV does handle fields with embedded newlines. In your output code, you could have used Text::CSV's print method. (By the way, why split $line only to join it again immediately afterwards?)

    Note that the last post in this thread was ~10 years ago, a lot has changed since then.

      Thanks for feedback, Mr Anon :)
      Multiline, meaning embedded newlines, yes - and you are right that the post is old, but since I am closing down a project, browsing through some perl hacks I found this thread and thought it worth while to comment on just the same.
      You might be right though that I was not installing the latest Text::CSV when making this script

      As for the method at parsing and printing, well I had to do some field manipulation both during import (unsorted) and export (sorted) for which I thought my example could be of use to someone else, struggling at CSV file manipulations :)
      Updated the examples accordingly, to better illustrate this

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2021-12-04 02:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (30 votes). Check out past polls.

    Notices?