use Text::CSV_XS; use IO::File; my $csv = new Text::CSV_XS({sep_char=>"\x1E", escape_char=>"\x10"}); my $io = new IO::File('< flat_file.txt'); until ($io->eof) { my $line = $io->getline(); $line =~ s/_UNSC/\x10/g; $line =~ s/(?<!\x10) _ /\x1E/g; my $column = $csv->parse($line); ## .. do something with the ArrayRef $column .. ## }

This code reads each line (I assume each line is a record), converts escape characters to \x10 (ASCII 'Data link escape') and your record-separator string to \x1E (ASCII 'Record separator') in memory, then passes that much-cleaner line to the Text::CSV_XS object for parsing.

This way, your manager won't see it as file conversion, because you're merely caching a simplified version in memory from which to work. You can reverse the substitution process if you need to save a new file in the same format.

Of course, once you've got code like this working, it would be trivial to write the file back out in a newer format whenever your boss changes his/her mind.

<-radiant.matrix->
Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
The Code that can be seen is not the true Code

In reply to Re: DBD::CSV and really bad legacy flat file by radiantmatrix
in thread DBD::CSV and really bad legacy flat file by harleypig

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.