in reply to Commas in quoted CSV records

Text::CSV_XS
while (<$fh_in>) { $csv->parse($_) or die("csv: " . $csv->error_diag() . "\n"); my @fields = $csv->fields(); if ($fields[9] == 32) { print $fh_kept $_; } else { print $fh_rejected $_; } }

Update: The OP talks about sorting the records, but he seems to just wants to filter them? Updated code.

Replies are listed 'Best First'.
Re^2: Commas in quoted CSV records
by Tux (Canon) on Mar 25, 2009 at 07:43 UTC

    Please don't promote parse () when reading from streams. This is error prone

    use strict; use warnings; use Text::CSV_XS; open my $h_in, "<", "S:/RFax-L7.txt" or die "RFAX: $!"; open my $h_sr, ">", "S:/sorted.csv" or die "Sorted: $!"; open my $h_rj, ">", "S:/rejected.csv" or die "Rejected: $!"; my $csv_in = Text::CSV_XS->new ({ binary => 1 }); my $csv_out = Text::CSV_XS->new ({ binary => 1, eol => "\r\n" }); while (my $row = $csv_in->getline ($h_in)) { if ($row->[9] == 32) { $csv_out->print ($h_sr, $row) } else { $csv_out->print ($h_rj, $row) } } $csv_in->eof or $csv_in->error_diag (); close $_ for $h_in, $h_sr, $h_rj;

    The prints that ikegami used will work fine too, but with the above code, you are more flexible, as you can alter the fields before writing them and still be sure the output is still valid.

    The OP sais Text::CSV_XS is already installed. Upgrading might give more functionality, but for a simple task like this you might not need it. To upgrade Text::CSV_XS you will need the matching installer. You seem to be on windows, reading your example, which means either ActivePerl or Strawberry, which will most likely also be somewhere in your START menu

    Strawberry

    C:> cpan Text::CSV_XS

    ActivePerl

    C:> ppm update Text::CSV_XS

    Enjoy, Have FUN! H.Merijn
Re^2: Commas in quoted CSV records
by generator (Pilgrim) on Mar 25, 2009 at 04:48 UTC
    Thanks for the reply. I'd love to use the package processes but I can't figure out how to apply it. I can't find any documentation that is explicit enough for an inexperienced PERL programmer like me to understand. I tried to insert the process you suggested but am getting errors related to "...requires explicit package name" on any number of lines. I'd love to find some brief discussion of the suggested Package function that would spell out the syntax and process. Any thoughts? Thanks again for chiming in. I feel like there is hope!

        The OP quest was not how to install the module, he already seems to have it available, but to find the manual pages, see his last paragraph:

        I have the Text-CSV_XS package but can't figure out how to use it.

        As I have no idea where the installed manual pages will turn up under windows, someone with more clues might drop in here, as the manual pages for Text::CSV_XS are stuffed with examples.


        Enjoy, Have FUN! H.Merijn
      I used $fh_in, $fh_kept and $fh_rejected for my file handles. You used something else. Adjust one or the other.
      If you don't know perl well enough, then go and buy a copy of "Learning Perl", work through it, then come back to your problem. You will find that far more effective than just asking here, because people will not just write all your code for you.
        Thank you so much for your "helpful suggestion".

        Incidently I own Learning Perl, Programming Perl, The Perl Cookbook and am currently slogging through Mastering Regular Expressions.

        I am not interested in having people write code for me but in understanding what the code is doing. "Give a man a fish..." and all that.

        Posting here broke the logjam on my problem. I came looking for a solution to one problem and was given an alternative approach. I'll probably still go back and try to clean up my regex to see why behavior in one program differed from behavior in another.

        In the mean time I can complete my "before the end of the month" project for a client so I'll have more free time for reading studying etc. Darned inconvenient this working for a living. It gets in the way of enlightenment.

Re^2: Commas in quoted CSV records
by generator (Pilgrim) on Mar 25, 2009 at 19:50 UTC
    Thank you for your reply. This works wonderfully. I've managed to find the documentation on Text::CSV_XS and am currently disecting your snippet so that I understand what it is doing. Thanks again for your assistance.