> I have an input file which has many subrecords. The subrecord type is 
> denoted by the first 4 characters of the file. The rest of the line is 
> formatted like similar to the way that "pack" would format one. That is, 
> each data point in a subtype is always at the same offset for the same 
> length. E.g. 10 characters starting at offset 30, or some such. What I'm 
> considering is using "unpack" and having a hash contain the unpack 
> template based on the subrecord type. Something like:
> 
> while (<FH>) {
>       my $subrec = substr($_,0,4);
>       my @values = unpack $template{$subrec}, $_;
> ...
> }
> 
> Earlier in the code, I would have created the %template hash which would 
> have the template associated with the $subrec from the input file.
> 
> Is this a decent way to do this? Is there a better way?
> 

Sounds pretty good to me.  One concern, do the sub record types always
have the same number of fields?  Using your array to unpack into may
turn into a maintenance nightmare with respect to indexing into it to
get values if the record formats are signficantly different, etc. 
Second concern, are you processing the records completely within the
loop or needing to parse them all before doing anything with them?  In
the latter case you may need to store them to an array based on type
rather than directly to a 'values' temporary array, etc.

For the first concern you may consider using a hash slice with the keys
being associated with the subtype stored in the original hash where you
retrieve the record format from.

Obviously there is also the potential to use objects here but that may
be overkill depending on what you are doing with the data after you have
unpacked it....

http://danconia.org


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to