Re: Iterating over hash to find specific key to sum up the costby kcott (Bishop)
|on Oct 29, 2021 at 23:38 UTC||Need Help??|
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.
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).