When you see an index like [0] in Perl code, this is a RED alert that probably something is wrong, not always but, more likely than not!!

You have an array of "pointers" to anon hashes. Data::Dumper is a SUPER cool thing to help visualize what is going on.

Below, I have a foreach loop that processes every hash_ref in @rec.
$hash_ref->{'name'} yields the name in that record
@{$hash_ref->{'kids'}} is a bit trickier. If this was just one kid, then it would work like a name. But there are multiple kids and this is an array of kids.
Below I show how to print @demo. $hash_ref->{'kids'} fetches one thing just like accessing $hash_ref->{'name'} did. But now we have to de-reference that single thing which is a "pointer" to array. In Perl when you have a "subscripted thing" and you want to de-reference it, you need to enclose it in brackets,{}. I did that, then I said that this is an array de-reference '@' and bingo, this is now just like printing @demo. In other words, you need to tell "@" what to operate upon. @$hash_ref->{'kids'} would mean something completely different. I want "@" to operate not just upon the value $hash_ref, but upon the whole thing that {$hash_ref->{'kids'}} "points to".

An array in a scalar context is the number of things in that array. I think others have covered that point.

#!/usr/bin/perl -w use strict; use Data::Dumper; my @demo =('xyz', 'qrs'); my @rec = ( { name => 'Nancy', address => 613, kids => ['abc', 'def'], }, { name => "Betty", address => 845, kids => ['xyz', 'qrs'], }, ); print Dumper \@rec; print "DEMO: @demo\n"; foreach my $hash_ref (@rec) { print "$hash_ref->{'name'} struggles with: ", "@{$hash_ref->{'kids'}}\n"; } __END__ $VAR1 = [ { 'name' => 'Nancy', 'kids' => [ 'abc', 'def' ], 'address' => 613 }, { 'name' => 'Betty', 'kids' => [ 'xyz', 'qrs' ], 'address' => 845 } ]; DEMO: xyz qrs Nancy struggles with: abc def Betty struggles with: xyz qrs

In reply to Re^3: print array of hashes by Marshall
in thread print array of hashes by fionbarr

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.