Hey Monks,

I'm having an issue with a script I wrote trying to populate an array of hashes.

I don't quite know why it's not working, most likely I just need an extra set of eyes to help me out.

What I'm trying to accomplish is the following result:
$VAR1 = { 'BNA' => 'Medium Batch 1 Department', 'SNO' => '1', 'products' => [ { 'PNO' => '5001', 'CCO' => 'SPIA', 'DNO' => '1' }, { 'PNO' => '5004', 'CCO' => 'SPIA', 'INO' => '5004', 'DNO' => '1' }, { 'PNO' => '5100', 'CCO' => 'SPIA', 'INO' => '5100', 'DNO' => '1' } ], 'BNO' => '669' }; $VAR2 = { 'BNA' => 'Medium Batch 2 Department', 'SNO' => '1', 'products' => [ { 'PNO' => '5001', 'CCO' => 'SPIA', 'DNO' => '1' }, { 'PNO' => '5004', 'CCO' => 'SPIA', 'INO' => '5004', 'DNO' => '1' }, { 'PNO' => '5100', 'CCO' => 'SPIA', 'INO' => '5100', 'DNO' => '1' } ], 'BNO' => '670' };
I'm trying to achieve this with the following code, but it just doesn't seem to be working out for me, any ideas?
#!perl use strict; use Data::Dumper; use warnings; my $Delimiter = chr(253); my %HostHash; my (@HostArray, @ProductArray); my $FIRST = 0; HOSTPARSE: while (<DATA>) { s/^\s+|\s+$//g; my @fields = split(/$Delimiter/, $_); my %fields; foreach my $field (@fields) { my $a1 = substr($field, 0, 3); my $a2 = substr($field, 3); $fields{$a1} = $a2 if $a2; } if ($fields{BNA}) { print "$fields{BNA} \n"; if ($FIRST) { print "Storing Products in Batch $HostHash{BNA}\n"; $HostHash{products} = \@ProductArray; @ProductArray = (); push @HostArray, \%HostHash; } else { $FIRST = 1; } %HostHash = %fields; next HOSTPARSE; } push @ProductArray, \%fields; } print "Storing Products in Batch $HostHash{BNA}\n"; $HostHash{products} = \@ProductArray; push @HostArray, \%HostHash; print Dumper @HostArray; __DATA__ BNAMedium Batch 1 DepartmentýBNO669ýSNO1ý CCOSPIAýDNO1ýPNO5001ýSAD0ýINO0ý CCOSPIAýDNO1ýPNO5004ýSAD0ýINO5004ý CCOSPIAýDNO1ýPNO5100ýSAD0ýINO5100ý BNAMedium Batch 2 DepartmentýBNO670ýSNO1ý CCOSPIAýDNO1ýPNO5001ýSAD0ýINO0ý CCOSPIAýDNO1ýPNO5004ýSAD0ýINO5004ý CCOSPIAýDNO1ýPNO5100ýSAD0ýINO5100ý
Thanks in advance!

In reply to Arrary of Hashes populating issue by SmokeyB

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.