I'm working on a problem where I have to produce a fixed-length string (record) that is comprised of N columns, each of varying width.

ie.:

my $str = "AAABBBBBBBBC";

where:

  "AAA" represents the value for "Batch_Agency"
  "BBBBBBBB" represents the value for "Batch_Date"
  "C" represents the value for "Batch_Type"

I'm concerned with these two data:

Once I get the data structure right, I'll use pack to produce the format specification and the fixed-length record.

I began setting up my data structure by creating the array of anonymous hashes as shown below in method 1:

#! perl -w use strict; use Data::Dumper; my $transaction_record_fields_method1 = [ { 'Batch_Agency' => [ 1 .. 3] }, { 'Batch_Date' => [ 4 .. 11] }, { 'Batch_Type' => [ 12 .. 12] }, ];

This was not very useful, since I wasn't able to come up with a way access the field name and column position array using this method.

If I change the data structure, I can successfully access the required elements by using either method2 or method3:

# Accessing an AoA my $transaction_record_fields_method2 = [ [ 'Batch_Agency', [ 1 .. 3] ], [ 'Batch_Date', [ 4 .. 11] ], [ 'Batch_Type', [ 12 .. 12] ], ]; print Dumper($transaction_record_fields_method2->[2][0]); print Dumper($transaction_record_fields_method2->[2][1]); # Accessing a plain ol' AoH my $transaction_record_fields_method3 = [ { name => 'Batch_Agency', range => [ 1 .. 3], }, { name => 'Batch_Date', range => [ 4 .. 11], }, { name => 'Batch_Type', range => [ 12 .. 12], }, ]; print $transaction_record_fields_method3->[2]{name}, "\n"; print Dumper($transaction_record_fields_method3->[2]{range});

Since my knee-jerk reaction was to set up the data structure using method1, I'd like, if possible, for someone to explain to me how to access the fieldname and column position array ref for each field using method1.

Thanks!

Updated: Data structure in method2



In reply to Accessing an array of anonymous hashes by thezip

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.