thezip has asked for the wisdom of the Perl Monks concerning the following question:
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"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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Accessing an array of anonymous hashes
by mreece (Friar) on Mar 26, 2007 at 17:07 UTC | |
by thezip (Vicar) on Mar 26, 2007 at 17:17 UTC | |
by mreece (Friar) on Mar 26, 2007 at 18:02 UTC | |
by thezip (Vicar) on Mar 26, 2007 at 18:14 UTC | |
by eric256 (Parson) on Mar 26, 2007 at 18:41 UTC | |
|
Re: Accessing an array of anonymous hashes
by ferreira (Chaplain) on Mar 26, 2007 at 18:16 UTC | |
by thezip (Vicar) on Mar 26, 2007 at 18:50 UTC | |
|
Re: Accessing an array of anonymous hashes
by Not_a_Number (Prior) on Mar 26, 2007 at 18:54 UTC |