Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

G'day vaitor15,

Welcome to the Monastery.

[I see from previous replies that you have modified your post one or more times. In the version that I'm seeing, 430 as a total is incorrect; however, other comments indicate that this was correct at one time. It is impossible for us, and therefore you, to tell which solutions are valid. Please ensure that you understand the issues here (by reading "How do I change/delete my post?") and, in any future posts, clearly indicate updates.]

In my code below, I've laid out the data (as I currently see it) in a manner that makes it more readable and comprehensible. Your current costs (6300+300+30) total 6630.

#!/usr/bin/env perl use strict; use warnings; my $VAR1 = { '153-1' => { '19-4' => { 'cost' => '6300.00', 'cost2' => '630.00' }, '135-1' => { '68-4' => { 'cost' => '300.00', 'cost2' => '130.00' } }, '1069-9' => { }, '35-1' => { '28-4' => { 'cost' => '30.00', 'cost2' => '10.00' } }, } }; my $total = 0; get_total_cost($VAR1, \$total); printf "Total = %.2f\n", $total; sub get_total_cost { my ($hash_data, $sub_total) = @_; for my $key (keys %$hash_data) { if (ref $hash_data->{$key} eq 'HASH') { next unless keys %{$hash_data->{$key}}; get_total_cost($hash_data->{$key}, $sub_total); } else { if ($key eq 'cost') { $$sub_total += $hash_data->{cost}; } } } return; }

Note that this doesn't care how deep in the hash the 'cost' key is located. It also ignores 'cost2' keys, and any other variant of a key containing 'cost'; if that's not what you wanted, modify the $key eq 'cost' condition (if it's just keys starting with 'cost', index would be the most efficient; otherwise, a regex would probably be the best solution).

Output:

Total = 6630.00

— Ken


In reply to Re: Iterating over hash to find specific key to sum up the cost by kcott
in thread Iterating over hash to find specific key to sum up the cost by vaitor15

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-03-29 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found