Ain't financial data vendor file formats fun?

For what it's worth, here's what I usually do when confronted with stuff like this: (well, sort of - I actually have a few modules now that make this kind of thing easier)

my %recordformat = ( '__COMMON__' => [qw( RequestYear 1-2 RequestMonth 3-4 RequestDay 5-6 RequestSequence 7-10 RecordType 11-12 RecordSequence 13-14 )], 94 => [qw( Destination 15-22 DestinationType 23-26 )] # other stuff ); sub fillHashFromData { my ($destination, $recordSpec, $data) = @_; my @spec = @$recordSpec; # make a copy while (@spec) { my $key = shift @spec; my $location = shift @spec; $location =~ /(\d+)(?:-(\d))?/ or die "Syntax error in recordSpec: @$recordSpec"; my ($start, $end) = ($1, $2 || $1); $destination->{$key} = substr($data, $start - 1, $end - $start + 1 +); } } # Much later.... my $dataLine; while ($dataLine = <VENDORFILE>) { my %datavalues; fillHashFromData(\%datavalues, $recordformat{__COMMON__}, $dataLine) +; fillHashFromData(\%datavalues, $recordformat{$datavalues{'RecordType +'}}, $dataLine); # Do stuff here }
Now, if you really, really wanted things as plain variables instead of hash entries, that's easily done in the Do stuff here section:
{ no strict qw(vars refs); while(my ($k,$v) = each %datavalues) {$$k = $v;} }
However, I'd stay away from that, since keeping things in a hash has the advantage that if you want it's very easy to enumerate the fields.

One distinct advantage of using a %recordformat hash in the format above is that a section of it can be cut-and-pasted directly from the vendor's documentation; also, it becomes easy for you to remove a huge number of fields that you're ignoring from consideration without making certain that you have the right number of "x" specifications in the pack string. (Occasionally vendors will give field lengths; in my experience, they always give character position ranges)

The subroutine that lets you construct a data line from a hash and a format is also pretty easy:

sub fillDataFromHash { my ($src, $recordSpec, $data) = @_; $data ||= ' ' x $RECORD_LENGTH; # Maybe add to %recordformat my @spec = @$recordSpec; while (@spec) { my $key = shift @spec; my $location = shift @spec; $location =~ /(\d+)(?:-(\d))?/ or die "Syntax error in recordSpec: @$recordSpec"; my ($start, $end) = ($1, $2 || $1); substr($data, $start - 1, $end - $start + 1) = $src->{$key}; } } # ... # fill %datavalues somehow my $dataLine = fillDataFromHash(\%datavalues, $recordformat{__COMMON__}); $dataLine = fillDataFromHash(\%datavalues, $recordformat{$datavalues{'RecordType'}}, + $dataLine);

In reply to Re: Dreaded Symbolic References by fizbin
in thread Dreaded Symbolic References by SkipHuffman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.