Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by tybalt89 (Monsignor)
on Oct 29, 2021 at 20:59 UTC ( [id://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?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found