Invest time in reading about data structures, Perl Data Structure Cookbook -perldsc - can be a good start, these come in handy and they are not difficult but are not easy too...

The function ref can allow you to make many useful decision with respect to the type of dereferencing task appropriate for a given segment of the data structure, for example, if 'ref' returned 'ARRAY' when you are looping through a hash you could probably access that particular array by dereferencing it as

foreach my $element (@{$hash{$key}}{ #work on $element; }

uncomment lines to see the returned value of 'ref' and compare that to the subsequent dereferencing approach chosen...
use strict; use warnings; use Data::Dump qw(pp); my %hash; push @{$hash{'article'}},{}; $hash{'article'}[0]= {'SKU'=> ['CDS0013'], 'InternalSKU'=>'179', 'Avai +lableItems'=>['100']}; $hash{'article'}[1]= {'SKU'=> ['CDS0014'], 'InternalSKU'=>'180', 'Avai +lableItems'=>['102']}; foreach my $key (keys %hash){ #print ref $hash{$key},"\n"; foreach my $element (@{$hash{$key}}){ # print ref $element,"\n"; $articleCount = scalar @{$hash{$key}}; #get the coun +t ... foreach my $innerKey(keys %{$element}){ print "$element->{$innerKey}","\n"; #some mor +e referencing yet } } } print "there are $articleCount articles\n"; print pp \%hash;

Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

In reply to Re: Having trouble looping through a data structure by biohisham
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.