http://www.perlmonks.org?node_id=1029960

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

Hi Monks
I saw this code here and I was trying to understand it, how if I would like to print just the types and the names from this data structure, tried it, but could not do it:
use strict; use warnings; my $array = [ { 'name' => 'Discount', 'reference' => '100 ', 'type' => 'Paper' }, { 'name' => 'Documents', 'reference' => '100 ', 'type' => 'Paper' }, { 'name' => 'Money', 'reference' => '340 ', 'type' => 'Plastic' }, { 'name' => 'State', 'reference' => '40 ', 'type' => 'Cotton' }, { 'name' => 'Slice', 'reference' => '30 ', 'type' => 'Cotton' }, { 'name' => 'Part', 'reference' => '45 ', 'type' => 'Cotton' }, ]; my %hash; #$hash{$_->{'type'}} {$_->{'name'}} = $_->{'reference'} for @$array; $hash{$_->{'type'}}{ $_->{'name'} }for @$array; print "Results:\n"; for my $k1 (sort keys %hash) { print " $k1:\n"; for my $k2 (sort keys %{$hash{$k1}}) { print " $k2 - $hash{$k1}{$k2}\n"; } }
I would like to get this:
Output: Results: Cotton: Part Slice State Paper: Discount Documents Plastic: Money
Thanks for helping!

Replies are listed 'Best First'.
Re: Understading array of hashes
by LanX (Saint) on Apr 22, 2013 at 20:17 UTC

    I think you are looking for a hash of arrays

    DB<126> $array => [ { name => "Discount", reference => "100 ", type => "Paper" }, { name => "Documents", reference => "100 ", type => "Paper" }, { name => "Money", reference => "340 ", type => "Plastic" }, { name => "State", reference => "40 ", type => "Cotton" }, { name => "Slice", reference => "30 ", type => "Cotton" }, { name => "Part", reference => "45 ", type => "Cotton" }, ] DB<127> push @{ $hash{$_->{type}} }, $_->{name} for @$array; DB<128> \%hash => { Cotton => ["State", "Slice", "Part"], Paper => ["Discount", "Documents"], Plastic => ["Money"], }

    update

    DB<136> use YAML DB<137> print Dump \%hash --- Cotton: - State - Slice - Part Paper: - Discount - Documents Plastic: - Money

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    PS: Starting with Perl 5.14, push can take a scalar EXPR, which must hold a reference to an unblessed array. ... i.a.W. you won't need the @{...} part.

Re: Understading array of hashes (homework)
by LanX (Saint) on Apr 22, 2013 at 21:20 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Understading array of hashes
by kennethk (Abbot) on Apr 22, 2013 at 19:43 UTC
    warnings gives you a hint with: Useless use of hash element in void context at fluff.pl line 43. In your for loop, you never actually assign anything, so it is a no-op. You get something very close if you uncomment line 41, so you should probably just concentrate on how to modify your output. Essentially, if you don't want to output something, then don't print it:
    my %hash; $hash{$_->{'type'}} {$_->{'name'}} = $_->{'reference'} for @$array; #$hash{$_->{'type'}}{ $_->{'name'} }for @$array; print "Results:\n"; for my $k1 (sort keys %hash) { print " $k1:\n"; for my $k2 (sort keys %{$hash{$k1}}) { print " $k2\n"; } }

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.