Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

array of hashes

by fionbarr (Friar)
on Oct 02, 2008 at 14:55 UTC ( [id://715029]=perlquestion: print w/replies, xml ) Need Help??

fionbarr has asked for the wisdom of the Perl Monks concerning the following question:

given the following hash how do I print the number of children? use Data::Dumper; %barry = ( name => Barry, age => 67, children => [ # anon array of hashes { # anon hash 1 name => Jenny, age => 38, education => BA }, { name => Brendan, age => 20, education => student } ] ); for $i (0 .. 1) { print $barry{children}->[$i]->{name} . "\n"; print $barry{children}->[$i]->{age} . "\n" ; print $barry{children}->[$i]->{education} . "\n" ; print "-----------------------\n"; } print Dumper(%barry);

Replies are listed 'Best First'.
Re: array of hashes
by kyle (Abbot) on Oct 02, 2008 at 15:12 UTC
      just right, thanks
Re: array of hashes
by eighty-one (Curate) on Oct 02, 2008 at 15:19 UTC
    First, read How do I post a question effectively? and Writeup Formatting Tips - your question would be much easier to read with >code< tags.

    As for your question, I found The Perl hash How-To very informative back when I found hashes & hashrefs confusing. Since children is an array, you should be able to just iterate over it with a foreach and perform any action that needs to be taken on each child, and increment a counter to count. Also, you can refer to an array in scalar context to get a count.
       code it is, sorry, and thanks
Re: array of hashes
by toolic (Bishop) on Oct 02, 2008 at 15:18 UTC
    Your code is difficult to understand because it was not wrapped in "code" tags. Please modify your node to add code tags (see Writeup Formatting Tips).

    Next, please use warnings; use strict;, then clean up any errors and warnings.

    That being said, I tried using perltidy to clean up your code the best I could, and I still got an error:

    Can't call method "0" on unblessed reference

    How many children do you expect in your code? 1?

    my $cnt = 0; for (keys %barry) {$cnt++ if $_ eq 'children'} print "cnt=$cnt\n"; __END__ cnt=1
Re: array of hashes
by NetWallah (Canon) on Oct 02, 2008 at 21:20 UTC
    Here's an OO implementation that I consider more readable --
    use strict; my $barry = new Person ("Barry", 67); $barry->AddKid( new Person("Jenny",38,"BA") , new Person("Brendan",20,"student") ); print "--- $barry->{NAME} 's " . $barry->getKidCount . " children ---\n"; for my $kid(@{$barry->{CHILDREN}}){ print $kid->{$_} ."\t" for qw[NAME AGE EDUCATION]; print "-----------\n"; } #----------------------------- INIT{ package Person; use strict; sub new{ my ($class,$name,$age,$edu) = @_; my $self= bless { NAME=>$name||"Anonymous", AGE=>$age||0, CHILDREN =>[], EDUCATION=>$edu || "", }, $class; return $self; } sub AddKid{ my $self = shift; push @ {$self->{CHILDREN} }, @_; } sub getKidCount{ my $self = shift; return scalar @{$self->{CHILDREN}}; } 1 }

         Have you been high today? I see the nuns are gay! My brother yelled to me...I love you inside Ed - Benny Lava, by Buffalax

Re: array of hashes
by ikegami (Patriarch) on Oct 02, 2008 at 20:05 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://715029]
Approved by kyle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found