in reply to Dreaded Symbolic References

SkipHuffman,
Try something like the following:
#!/usr/bin/perl use strict; use warnings; my $foo; # Replace with $_ accordingly my %data; my %table = ( 94 => "A8A40" ); my @fields = qw(Year Month Day Request_Seq Request_Type Record_Seq Rec +ord_Data); @data{ @fields } = unpack "A2A2A2A4A2A2A1486" , $foo; my ($Destination, $DestinationType) = unpack $table{$data{Record_Type} +} , $data{Record_Data};
Cheers - L~R

Replies are listed 'Best First'.
Re: Re: Dreaded Symbolic References
by SkipHuffman (Monk) on Mar 08, 2004 at 17:21 UTC

    Beauty!

    That is exactly the direction that I needed. My code is now like this:

    my %Field; my @HeaderFields=qw(RequestYear RequestMonth RequestDay RequestSequenc +e RecordType RecordSequence Record); my %RecordMap =(Preface => A2A2A2A4A2A2A1486, 94 => A8A40); ... @Field{@HeaderFields}=unpack("$RecordMap{Preface}",$_); ...

    Now just to change the rest of the mess into the nice clean hashes. (but first, lunch!)

    Thanks LR (and all the other monks)

    Skip

      In order to avoid potential mismatches of the field name and pack template values, I would think about using a hash to generate both the unpack template and the field names. The only problem is hashes are not stored in any particular order. To combat this, one could store the "hash" in an array, and separate the keys and values "manually." Example:

      my @fields = qw(Year A2 Month A2 Day A2 Request_Seq A4 Request_Type A2 Record_Seq A2 Record_Data A1486); my @keys = map $fields[$_], grep $_%2==0, 0 .. $#fields; my @values = map $fields[$_], grep $_%2==1, 0 .. $#fields; my %data; # bad name. we should know better. ;) @data{ @keys } = unpack join("", @values), $foo;

      Alternatives to this include, but are not limited to:

      • Use a tied hash that maintains order, such as Tie::IxHash.
      • Use a normal hash, but also keep along an array that is used to sort the hash keys and values. This is essentially what a tied hash would do for you, automatically.
      • Some other extremely clever solution I have not thought of.