Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://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

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 having a coffee break in the Monastery: (7)
As of 2024-03-28 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found