Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: using ref to hash of hash effectively

by AnomalousMonk (Archbishop)
on Dec 27, 2020 at 10:26 UTC ( [id://11125782]=note: print w/replies, xml ) Need Help??


in reply to Re: using ref to hash of hash effectively
in thread using ref to hash of hash effectively

Since the weapon names don't seem to matter, only their attributes, I think it's simpler to iterate over the values of the weapons hash per Cristoforo's approach. This works with map, but I think it's even simpler (and IMHO clearer) to use for-loops:

Win8 Strawberry 5.8.9.5 (32) Sun 12/27/2020 5:07:00 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings -l use Data::Dumper; my $weapons_ref = { dagger => { cost => 8, damage => 4, armor => 0, }, shortsword => { cost => 10, damage => 5, armor => 0, }, warhammer => { cost => 25, damage => 6, armor => 0, }, longsword => { cost => 40, damage => 7, armor => 0, }, greataxe => { cost => 74, damage => 8, armor => 0, }, }; my $sum_result; my @sum_fields = qw/cost damage armor/; # map { # works # my $attrib = $_; # map { $sum_result->{$_} += $attrib->{$_} } @sum_fields # } values %$weapons_ref; for my $hr_attrib (values %$weapons_ref) { $sum_result->{$_} += $hr_attrib->{$_} for @sum_fields; } print Dumper $sum_result; ^Z $VAR1 = { 'cost' => 157, 'damage' => 30, 'armor' => 0 };


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: using ref to hash of hash effectively
by kalee (Initiate) on Dec 27, 2020 at 14:55 UTC

    This is definitely the direction I was looking for! Both of these!

    The problem is part of adventofcode dot com - 2015 day 21, so not a homework problem per say, and I've already solved it in another language. This is really me wanting to learn how to use arrays and hashes of stuff more effectively.

    I really should have added that this is a Data Structure/Algorithm problem. In general, I want to learn how to structure the data so I can slice and dice it better. Leaving the data in two nested anonymous hashes isn't necessarily the goal. Rearranging the data to easily manipulate it would definitely be best!

    I have been trying to figure out how I could use List::AllUtils to get groups of data out of this grouping or set of data. Though, changing the initial structure may be a better solution. I don't know what I don't know. That is kind of the open question. When you get a set of data like this, and please look at this fun and interesting problem, is there some sort of best practice for what you check for to decide what type of data structures to put the data into? That would truly be the magical part!

      In general, I want to learn how to structure the data so I can slice and dice it better.
      Leaving the data in two nested anonymous hashes isn't necessarily the goal.
      Rearranging the data to easily manipulate it would definitely be best!
      Some famous programmers who agree with you

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-20 11:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found