in reply to Seperating Fixed Line Feed into Fields

Further to the good advice you have been given regarding the use of unpack and arrays, there are a few other potential issues with your code.

Firstly, your local $/ ... is not really local unless you confine the scope to a bare block, e.g.

... { local $/ = \1173; ... } ...

Getting into the habit of doing this will minimise the risk of unwanted side effects if reading from another file handle later in the script.

Secondly, I would recommend the use of the three argument form of open and lexical rather than package filehandles. Also, you only check for success on one of your open statements and neither of your closes. E.g.

... my $inputFile = q{C:/INPUTFILE}; open my $inputFH, q{<}, $inputFile or die qq{open: < $inputFile: $!\n}; my $outputFile = q{C:/OUTPUT.TXT}; open my $outputFH, q{>}, $outputFile or die qq{open: > $outputFile: $!\n}; ... while( <$inputFH> ) ... print $outputFH ... ... close $inputFH or die qq{close: < $inputFile: $!\n}; close $outputFH or die qq{close: > $outputFile: $!\n};

Thirdly, you should consider separating the skipping of the first seven records from reading the rest of the records so as to avoid testing whether to skip for every read, and do you actually use $rcount for anything other than the record skipping?. I'm not sure of the logic you use when incrementing $rcount so below I show the way I'd tackle it.

... my $rcount; { local $/ = \1173; my $discard = <$inputFH> for 1 .. 7; $rcount = 7; # We've seen and skipped 7 records so far. while( <$inputFH> ) { $rcount ++; my @fields = unpack q{A34 ... }, $_; ... } } ... # Do something with $rcount here? ...

I hope these points are of interest.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Seperating Fixed Line Feed into Fields
by drodinthe559 (Monk) on Jun 11, 2009 at 17:51 UTC
    Thanks, JohnGG. Your suggestions made a huge improvement to the script. Moreover, you showed me several better techniques I can utilize. When I first started writing it, I knew it could be way better; hence the posting. I think I am going to take a breather now and work on generating the file to excel. Since you skipped the set of rows, I can leave out $rcount. Below is how it looks now. Thanks, Dave
    use strict; use warnings; use Win32::OLE; my $ex = Win32::OLE->new('Excel.Application') or die "oops\n"; my $book = $ex->Workbooks->Add; my $sheet = $book->Worksheets(1); my $inputFile = q{c:/INPUT.TXT}; open my $inputFH, q{<}, $inputFile or die qq{open: < $inputFile: $!\n} +; my $outputFile = q{C:/OUTPUT.TXT}; open my $outputFH, q{>}, $outputFile or die qq{open: > $outputFile: $! +\n}; { local $/ = \1173; my $discard = <$inputFH> for 1 .. 7; while (<$inputFH>) { $rcount ++; $sheet->Range("A1:CD100")->{value} = unpack("A34 A30 A40 A40 +A40 A40 A40 A2 A9 A9 A6 A40 A34 A3 A8 A1 A1 A10 A20 A10 A34 A15 A15 A15 A8 A8 A8 A8 A3 A3 A +40 A10 A7 A20 A7 A15 A5 A8 A20 A15 A15 A15 A15 A15 A15 A1 A4 +0 A5 A8 A1 A3 A3 A3 A20 A10 A1 A15 A15 AA50 A1 A8 A1 +0 A1 A7 A3 A7 A1 A1 A15 A8 A15 A3 A15 A8 A20 A6 A6 A +20 A15 A15 A15 A40 A15", $_); } } close $inputFH; close $outputFH; $book->SaveAs('C:\test.xls'); undef $book; undef $ex;