in reply to Parsing text file to CSV

Thanks to the power of regexes and split and Text::CSV, it is really easy:
use Modern::Perl; use Text::CSV; my $important; while (<DATA>) { chomp; last if m/-------------------------------------------------------- +-----------------------------------------/; next unless m/Important Text: (.*)/; $important = $1; } open my $fh, '>', 'output.csv'; my $csv = Text::CSV->new({eol => "\n", quote_null => 1,}); # check the + docs for the parameters to set the format of the CSV while (<DATA>) { chomp; my @data = ($important, split /\s+/)[0..6]; # we only need "import +ant" + the first 6 fields of the data $csv->print ($fh, \@data); } close $fh; __DATA__ Date 08/17/11 Report Page 1 Time 12:46 Important Text: 1 Misc Text: All Misc Text: Sec ** Indicates APPTEXT PLINE SCODE PCODE FID SEC unsec fcs ---------------------------------------------------------------------- +--------------------------- TEST TT TT00 TT00.1 NO xxxx TTD TEST TT TT00 **TT00.2 YES XXXXXX TEST TT TT00 **TT00.3 YES XXX TEST TT TT01 TT01.1 NO XXXXXXXXXXX TT TEST TT **TT02 TT02.1 YES XXXXX

Output:

1,TEST,TT,TT00,TT00.1,NO,xxxx 1,TEST,TT,TT00,**TT00.2,YES,XXXXXX 1,TEST,TT,TT00,**TT00.3,YES,XXX 1,TEST,TT,TT01,TT01.1,NO,XXXXXXXXXXX 1,TEST,TT,**TT02,TT02.1,YES,XXXXX

In real production code you will want to check --at the very least-- that each input and output function succeeded.

Update: I missed that you only needed the first 6 fields of your data. Solved that by using an array slice.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James