David92 has asked for the wisdom of the Perl Monks concerning the following question:
I am struggling with a problem. I have a hash, example:
$myHash{$customer}{money}The $customer is a value that I get by analizing a database everyday. This means, that everytime I run the script, there might be a new customer. The result of the hash is a scalar (money they spent).
I want to write a procedure how to extract the money they spent, by each $customer. I tried somehow like this:
foreach $customer (keys %myHash){ $money_spent = $myHash{$customer}{money}; }
Ofcourse, I am aware this doesnt work as i want, because the $money_spent will change everytime another $customer comes.
But I don't know how to get it done efficiently. I DO NOT WANT TO USE IF STATEMENTS HERE, LIKE THIS:
foreach $customer (keys %myHash){ print "$customer \n"; #--check what customers are there if ($customer eq 'NAME-OF-CUST'){ $money_spent_name_of_cust = $myHash{$customer}{money}; } ... }
I dont want to manually write down an IF statement for every $customer. There has to be a way, to store the values for each customer into a variable $money_spent.
Ideas very appreciated. David
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Keys and Values from Hash
by AppleFritter (Vicar) on Jul 18, 2014 at 12:48 UTC | |
by David92 (Sexton) on Jul 18, 2014 at 12:56 UTC | |
by AppleFritter (Vicar) on Jul 18, 2014 at 13:02 UTC | |
by David92 (Sexton) on Jul 18, 2014 at 13:06 UTC | |
by AppleFritter (Vicar) on Jul 18, 2014 at 13:22 UTC | |
by poj (Abbot) on Jul 18, 2014 at 17:35 UTC | |
|
Re: Keys and Values from Hash
by roboticus (Chancellor) on Jul 18, 2014 at 17:38 UTC | |
|
Re: Keys and Values from Hash
by Laurent_R (Canon) on Jul 18, 2014 at 17:53 UTC |