Yikes! That code didn't get wrapped for some reason, and
it's throwing the navigation table way off kilter. Anyway.
The code you posted is really Perl 4 style, with a whole
whack of arrays instead of the Perl 5 style Array of Arrays
(or AoA as you will hear more often). AoA is a much easier
way to implement what you have done. Easier is better, no?
I would define your input file format, first, in a structure,
and then write a loop to use this information to re-parse
the file. Consider making an array that has only the start
positions of each of the fields:
# Define the format of the file
my (@file_format) = (
0,
3,
53,
60,
67,
74,
# etc.
241+169, # Last position, presumably
);
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.
Now you can put each line into an array as you read it in,
and then write it to a file straight away. Just open both
files at the same time using two different filehandles,
such as IN_FILE and OUT_FILE. You are putting
your data into temporary arrays, but since the data is only
used exactly once.
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);
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).
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.