a second approach to the one above that refactors it a little bit -- hopefully will make it a little clearer on how the structure is traversed.
while( my ($s, $u_arr) = each %test ){
foreach my $u_idx ( 0..$#$u_arr ){
my $u = $u_arr->[$u_idx];
while( my ($n, $value) = each %$u ){
print "$s $u_idx $n: $value - \n";
}
}
}
And if you don't care about the index, it can be:
while( my ($s, $u_arr) = each %test ){
foreach my $u ( @$u_arr ){
while( my ($n, $value) = each %$u ){
print "$s $n: $value - \n";
}
}
}
Also, the naming of the temp variables (e.g. $user instead of $u, etc) in this case can make a huge difference in trying to figure out where you are in the complex data structure (or when you revist this code at a later date).
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.