I'd say add a header line and use DBD::CSV. Of course you can do it with Text::CSV or Text::CSV_XS.

I'd use DBD::CSV if the dataset isn't too large (the whole set fits easily in memory).

I'd use Text::CSV_XS' csv function with on_in that lets you change fields on the fly.

$ cat test.pl #!/pro/bin/perl use 5.20.0; use warnings; use Text::CSV_XS "csv"; my %state = qw( 0H Ohio 0R Oregon C CONDENSED F Final ); csv (in => "test.csv", on_in => sub { $_[1][0] = $state{$_[1][0]} || $_[1][0]; $_[1][4] = $state{$_[1][4]} || $_[1][4]; }); $ cat test.csv C,003187995,0H047013N,20141212,0H,2050,EX,, C,013139784,0H0430000,20141110,0H,2014,EX,, C,153188023,0H0020000,20150105,0H,2015,BA,, C,173167740,MT007015G,20141202,0R,2015,BA,, $ perl test.pl CONDENSED,003187995,0H047013N,20141212,Ohio,2050,EX,, CONDENSED,013139784,0H0430000,20141110,Ohio,2014,EX,, CONDENSED,153188023,0H0020000,20150105,Ohio,2015,BA,, CONDENSED,173167740,MT007015G,20141202,Oregon,2015,BA,, $

update: As of version 1.17, you can also use filter (if you prefer that):

csv (in => "test.csv", filter => { 1 => sub { $_ = $state{$_} || $_ }, 5 => sub { $_ = $state{$_} || $_ }, });

Enjoy, Have FUN! H.Merijn

In reply to Re: Search/Replace within fields of CSV by Tux
in thread Search/Replace within fields of CSV by johnmck

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.