in reply to parsing CSV file with embedded commas (fortunately, fixed-width) - is unpack the solution?

Sounds to me like Text::ParseWords will do the trick.

use Text::ParseWords; my @items; while (<>) { push @items, [ quotewords(',', 0, $_) ]; }

It's part of the standard Perl distribution.

--
<http://www.dave.org.uk>

"The first rule of Perl club is you don't talk about Perl club."

  • Comment on Re: parsing CSV file with embedded commas (fortunately, fixed-width) - is unpack the solution?
  • Download Code

Replies are listed 'Best First'.
(tye)Re4: parsing CSV file with embedded commas (fortunately, fixed-width) - is unpack the solution?
by tye (Sage) on Nov 01, 2001 at 00:39 UTC

    Note that this will have problems (quite reasonably) dealing with the unescaped single quotes inside of single-quoted fields that amelinda's data appears to have.

            - tye (but my friends call me "Tye")

      Hmm... well you're right that it would break on unescaped single quotes, but looking at amelinda's data I couldn't see any. Double quotes yes, but not single quotes. That's why I sugggested it.

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you don't talk about Perl club."

        Yes, no such examples were shown. However, amelinda wrote:

        In some, other records, one of the other text fields has an embedded '.
        as well says in the title (and confirmed in chatter) that the fields are fixed-width, which means that the embedded ' very likely isn't doubled (or otherwise escaped). (:

                - tye (but my friends call me "Tye")