in reply to Converting fixed record length files to pipe delimited
Now the length of each field $n, for substr() purposes, at least, is simply $file_format[$n+1] - $file_format[$n]. Note that the last entry in the table shouldn't be used, that is, $n should only go as high as $#file_format-1.# Define the format of the file my (@file_format) = ( 0, 3, 53, 60, 67, 74, # etc. 241+169, # Last position, presumably );
If you want, you can use unpack instead, but apart from stylistic differences, there is no real point unless you need maximum speed (i.e for 5 million line files, or what have you).my (@field_data); for (my $i = 0; $i < $#file_format; $i++) { $field_data[$i] = substr($_, $file_format[$i], $file_format[$i+1]- $file_format[$i]); # Clean up as required, by trimming $field_data[$i] =~ s/\s+$//; } print OUT_FILE join('|', @field_data);
|
|---|