Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Understading array of hashes

by Anonymous Monk
on Apr 22, 2013 at 19:36 UTC ( #1029960=perlquestion: print w/ replies, xml ) Need Help??
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!

Comment on Understading array of hashes
Select or Download Code
Re: Understading array of hashes
by kennethk (Monsignor) 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.

Re: Understading array of hashes
by LanX (Canon) 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 (Canon) on Apr 22, 2013 at 21:20 UTC
      If you can't answer ones's questions at least do the right thing and do the link to whatever you think is right correct.
        click search, dumbhead!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-08-22 02:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (145 votes), past polls