in reply to Re: Dreaded Symbolic References
in thread Dreaded Symbolic References

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

Replies are listed 'Best First'.
Re: Re: Re: Dreaded Symbolic References
by revdiablo (Prior) on Mar 08, 2004 at 19:38 UTC

    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.