in reply to Text::CSV_XS and blank lines

This may be a situation where you're forced to forgo the use of the convenience function, getline_hr, and roll your own code to generate a hash of the field values keyed by field labels. I do this a lot myself because I'm forced to use a version of Text::CSV_XS that predates the introduction of getline_hr.

my $value_of; $csv->parse(); @value_of{ @ordered_field_labels } = $csv->fields();

Typically, I get the ordered field labels from the CSV file header.

my @ordered_field_labels; while (my $csv_record = <>) { $csv->parse($csv_record); my @values = $csv->fields(); if ($INPUT_LINE_NUMBER == 1) { @ordered_field_labels = @values; # ... } # ... }

It's not inelegant. It's just a little less convenient.

(The blank lines are outside any quoted strings, right? They're not literal blank lines within fields are they?)

Replies are listed 'Best First'.
Re^2: Text::CSV_XS and blank lines
by constantreader (Initiate) on Feb 03, 2011 at 22:29 UTC

    By "blank lines" I mean a line with nothing but a /n.

    Thanks for the sanity check.

      Your definition of what constitutes a blank line is understood. The question is about the context in which those blank lines occur within the file. Are the blank lines within or without quoted strings? In other words, are the blank lines part of the literal data?

      That's the sanity check.

        Doh! I misunderstood. Sorry. The blank lines are not part of the literal data.
Re^2: Text::CSV_XS and blank lines
by Tux (Canon) on Feb 04, 2011 at 07:11 UTC

    This code would break any data that has embedded newlines. You should not rely on perl's diamond operator in CSV parsing.


    Enjoy, Have FUN! H.Merijn