in reply to DBD::CSV and really bad legacy flat file

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

Replies are listed 'Best First'.
Re^2: DBD::CSV and really bad legacy flat file
by harleypig (Monk) on Jul 19, 2005 at 19:20 UTC
    Hmmm ... so with this I could subclass Text::CSV_XS and pass that subclass to DBD::CSV instead ... I think I can do that ... thanks.
    Harley J Pig