Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by LanX (Sage)
on Oct 29, 2021 at 19:30 UTC ( #11138235=note: print w/replies, xml ) Need Help??


in reply to Iterating over hash to find specific key to sum up the cost

your indentation is misleading, the cost entries are at different levels.

I was thinking of chaining 3 maps but thats not possible here.

I recommend using Data::Diver or Data::Leaf::Walker or the alternative recursive solutions listed in threads mentioning these modules.

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' } }, }, };

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: Iterating over hash to find specific key to sum up the cost (updated)
by haukex (Bishop) on Oct 29, 2021 at 19:33 UTC
    your indentation is misleading, the cost entries are at different levels.

    You're right that it's misleading, for my reply I assumed there were two closing braces missing right before '135-1' and '1069-9', in which case the cost keys would be at the same level, but your interpretation is possible as well.

    vaitor15: Please clarify your input data.

    Update: The code in the OP did not compile when I replied because there were missing closing braces. It seems that has now been fixed and you are correct about the nesting.

      anyway I don't understand the requirement "sums up to 430" either.

      FWIW: This old node shows a recursive walker to dive into a randomly nested structure.

      The OP might want to adapt it to his needs.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        anyway I don't understand the requirement "sums up to 430" either.

        See the last update at the top of my node: the cost keys did originally sum up to 430.

        Update:

        FWIW: This old node shows a recursive walker to dive into a randomly nested structure.

        I've also got some "diver" type code if you search for "dive" on my scratchpad.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11138235]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2022-05-17 07:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (65 votes). Check out past polls.

    Notices?