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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.