SmokeyB has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to achieve this with the following code, but it just doesn't seem to be working out for me, any ideas?$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' };
Thanks in advance!#!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ý
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Arrary of Hashes populating issue
by davido (Cardinal) on Jan 27, 2005 at 16:38 UTC | |
by SmokeyB (Scribe) on Jan 27, 2005 at 17:20 UTC | |
|
Re: Arrary of Hashes populating issue
by dragonchild (Archbishop) on Jan 27, 2005 at 16:45 UTC | |
|
Re: Arrary of Hashes populating issue
by sweetblood (Prior) on Jan 27, 2005 at 16:42 UTC |