Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Final Update: When I replied, the code in the OP did not compile, as one of the four closing braces of $VAR1 was missing, leading me to have to make an assumption of what the data structure looks like. The unmarked update to the OP has now invalidated my code below. Plus, the first nested hash was as shown below instead of the current '19-4' => { 'cost' => '6300.00', 'cost2' => '630.00' }, so the sum of the cost keys was indeed originally 430 as OP expects. vaitor15: It is uncool to update a node in a way that renders replies confusing or meaningless.

Note what you've posted here is not runnable. See How do I post a question effectively? Update 2: Because it's not runnable, I've had to guess as to what your data structure looks like, but as LanX points out in his reply, other interpretations are possible. Please clarify what your real input data looks like. /Update

You should read the Perl Data Structures Cookbook (in particular Access and Printing of a HASH OF HASHES) and the Perl References Tutorial to learn how to work with nested data structures like these. Basically, you can use keys to loop over the keys of each level of hashes - this should be enough to get you started:

use warnings; use strict; my %result = ( "1069-9" => {}, "135-1" => { "68-4" => { cost => "300.00", cost2 => "130.00" } }, "153-1" => { "19-4" => { cost => "100.00", cost2 => "30.00" } }, "35-1" => { "28-4" => { cost => "30.00", cost2 => "10.00" } }, ); for my $k1 ( keys %result ) { for my $k2 ( keys %{ $result{$k1} } ) { if ( exists $result{$k1}{$k2}{cost} ) { print "$k1 / $k2 / $result{$k1}{$k2}{cost}\n"; } } }

Update: I used exists in the above code to check for the existence of the cost key because you said "find sum of value when cost key is found". I should add that, depending on what values are possible in your input data, you may want to check for definedness or truth instead.

In reply to Re: Iterating over hash to find specific key to sum up the cost (updated!) by haukex
in thread Iterating over hash to find specific key to sum up the cost by vaitor15

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 or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2022-05-29 07:42 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (101 votes). Check out past polls.