I'm reading a tab-delimited file with column headings and
writing out to a fixed-length text file. The code I have
works, but I'm wondering if there isn't a better way to do
some of this.
I want to get each line of input into a hash with the tab-
delimited data fields as values and the column headings as
keys. This makes the code much more self-documenting. The
only way I've been able to do this is to split the column
headings to an array and split each data line to an array,
then use a loop to build the hash from the 2 arrays. Is
there a better way to do this? Like somehow split the
data lines so that the fields become the hash values without
using a loop?
Also, I'm using the substr function to put the data where
it's supposed to be in the fixed-length file. Unless I make
the output record much longer than it really needs to be, I
get the error message "Substring outside of string" when I
run the program. However, it doesn't appear to me that I'm
actually trying to put anything in positions beyond the end
of the string I've defined. Anyone know why this happens and how to get around it without making the output records very long?
Code excerpt follows:
writing out to a fixed-length text file. The code I have
works, but I'm wondering if there isn't a better way to do
some of this.
I want to get each line of input into a hash with the tab-
delimited data fields as values and the column headings as
keys. This makes the code much more self-documenting. The
only way I've been able to do this is to split the column
headings to an array and split each data line to an array,
then use a loop to build the hash from the 2 arrays. Is
there a better way to do this? Like somehow split the
data lines so that the fields become the hash values without
using a loop?
Also, I'm using the substr function to put the data where
it's supposed to be in the fixed-length file. Unless I make
the output record much longer than it really needs to be, I
get the error message "Substring outside of string" when I
run the program. However, it doesn't appear to me that I'm
actually trying to put anything in positions beyond the end
of the string I've defined. Anyone know why this happens and how to get around it without making the output records very long?
Code excerpt follows:
Code:
use constant OUTLEN => 900;
while (chomp($_= <>))
{
if ($. == 1) {
@headers = split(/\t/); #get col. headers
next;
}
@F = split(/\t/); #split input data to array
# Create hash %H with array @F as values and array
# @headers as keys.
for $i (0..$#F) {
$H{$headers[$i]} = $F[$i];
}
$out = " " x OUTLEN . "\n"; #initialize output record
# Put values from hash %H into fixed positions in output
# file.
substr($out,0,10) = $H{PHONE};
substr($out, 50,5) = $H{SEQ_ID};
substr($out,55,15) = $H{ID};
substr($out,70,20) = $H{FNAME};
substr($out,90,20) = $H{LNAME};
# etc ...
# Write output record
print $out
} [\code]
Any comments/suggestions much appreciated. Thanks.