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 |