One fishy thing about the example structure of the OP are sub-elements like
     'SKU' => [ 'CDS00013' ],
and
     'AvailableItems' => [ '100' ]

These imply there may be more than one value associated with each key, but I wonder if it is likely there will be more than one SKU number for a given item. If there will not be, don't put these elements in anonymous arrays.

Of course, the OP example may just be an extreme simplification of a much more complicated structure. If so, it may be worthwhile considering a re-organization of the structure. For instance, if the 'InternalSKU' represents one or more actual, 'external' SKUs (say, from different suppliers, each with its own inventory count), might something like the following be more appropriate for maintainability:

use warnings FATAL => 'all'; use strict; my %hash = ( article => [ { InternalSKU => '179', Name => 'wiggle', external_source => [ { mfg => 'AMD', SKU => 'CDS00013', count => 100, }, { mfg => 'Intel', SKU => 'FOO00099', count => 42, }, ], }, { InternalSKU => '180', Name => 'woggle', external_source => [ { mfg => 'AMD', SKU => 'CDS00014', count => 102, }, { mfg => 'Intel', SKU => 'BAR00042', count => 99, }, ], }, ], ); my $ref_to_article_anon_array = $hash{article}; for my $hashref (@$ref_to_article_anon_array) { my $i_sku = $hashref->{InternalSKU}; my $name = $hashref->{Name}; print "Name $name; Internal SKU: $i_sku; Source(s): \n"; my $grand_total = 0; for my $hr_external (@{ $hashref->{external_source} }) { # my $mfg = $hr_external->{mfg}; # my $sku = $hr_external->{SKU}; # my $count = $hr_external->{count}; my ($mfg, $sku, $count) = # either way works @$hr_external{ qw(mfg SKU count) }; $grand_total += $count; print " mfg $mfg; mfg's SKU: $sku; count: $count \n"; } print "total ${name}'s on hand: $grand_total \n\n"; }

Output:

Name wiggle; Internal SKU: 179; Source(s): mfg AMD; mfg's SKU: CDS00013; count: 100 mfg Intel; mfg's SKU: FOO00099; count: 42 total wiggle's on hand: 142 Name woggle; Internal SKU: 180; Source(s): mfg AMD; mfg's SKU: CDS00014; count: 102 mfg Intel; mfg's SKU: BAR00042; count: 99 total woggle's on hand: 201

In reply to Re: Having trouble looping through a data structure by AnomalousMonk
in thread Having trouble looping through a data structure by DreamT

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.