Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Iterating over hash to find specific key to sum up the cost

by tybalt89 (Prior)
on Oct 29, 2021 at 20:59 UTC ( #11138243=note: print w/replies, xml ) Need Help??


in reply to Iterating over hash to find specific key to sum up the cost

#!/usr/bin/perl use strict; use warnings; use List::Util qw( sum0 ); my $hashref = { "153-1" => { "1069-9" => {}, "135-1" => { "68-4" => { cost => "300.00", cost2 => "130.00" } +}, "19-4" => { cost => "6300.00", cost2 => "630.00" }, "35-1" => { "28-4" => { cost => "30.00", cost2 => "10.00" } }, }, }; print costof( $hashref ), " is the total where cost is found\n"; sub costof { my $href = shift; sum0 map /cost/ ? $href->{$_} : costof( $href->{$_} ), keys %$href; }

Outputs:

7400 is the total where cost is found

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2022-05-29 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?