in reply to Keys and Values from Hash

There has to be a way, to store the values for each customer into a variable $money_spent.

Do you want to store the value for each customer, or an aggregate? If the former, use a hash;

my %money_spent = (); foreach my $customer (keys %myHash) { $money_spent{$customer} = $myHash{$customer}->{"money"}; }

If the latter, simply sum the values:

my $money_spent = 0; foreach my $customer (keys %myHash) { $money_spent += $myHash{$customer}->{"money"}; }

You can also use map to do this in a more concise fashion. Per-customer hash:

my %money_spent = map { $_ => $myHash{$_}->{"money"} } keys %myHash;

Aggregate (using the sum function from List::Util):

use List::Util qw/sum/; # ... my $money_spent = sum map { $myHash{$_}->{"money"} } keys %myHash;

Or do you want to do something else entirely?

EDIT: note that the above snippets assume that %myHash looks something like this:

my %myHash = ( "Fred Flintstone" => { "money" => 1000, "wife" => "Wilma", }, "Barney Rubble" => { "money" => 2000, "wife" => "Betty", }, );

Replies are listed 'Best First'.
Re^2: Keys and Values from Hash
by David92 (Sexton) on Jul 18, 2014 at 12:56 UTC
    I apologize, I was probably unclear. I will try to explain it better:

    My goals is to get the appropriate value for it's appropriate key in Hash. For example.

    'CUSTOMER1' = 20; 'CUSTOMER2' = 40; 'CUSTOMER3' = 60;

    The above keys and values are in %myHash.

    Now I want to extract each of the value and store it in appropriate Variable. For example:

    $money_spent_customer1 = 20; $money_spent_customer2 = 40;

    The problem is, I only find the solution with knowing what are my $customer and typing an IF statement, to check if the current $customer matcher CUSTOMER1 (for example), etc.

    I was hopping if there is a better way to store the values inside variables seperetely for each $customer

    The end goal of all this is to draw a chart on my HTML page. To see, how much money did certain customer spend. And I want to be automated as possible - so that I tomorow we'll have new customers, I want PERL to generate my HTML without my manual interfering (apart of running the Script).

      So, if I understand correctly, for each customer -- that is to say, each key of %myHash -- you want a variable named after that customer?

      You could do that with soft references (note that this will fail to compile under use strict, and generate warnings even without it):

      my %myHash = ( "FredFlintstone" => 1000, "BarneyRubble" => 2000, ); foreach (keys %myHash) { $var = "money_spent_$_"; $$var = $myHash{$_}; } say $money_spent_FredFlintstone; # 1000 say $money_spent_BarneyRubble; # 2000

      But I'd strongly advise against this. This is precisely the sort of thing that hashes are for.

        Yes something similar like you wrote!! Do you suggest any better way to do that?

        My end goal is to create an HTML page, which will display a Column Chart. Each column is each $customer, who has its value drawn.

        In bussiness, we have everyday new customers, and so the myHash will change everyday when I run the Script to get the data (which customers did visit us today). That can be completely direferent than yesterday (the names of $customer).

        I am trying to find a good way to filter through myHash each $customer with it's representing value (money).

        I Should ADD impotant info:

        I noticed I have always written here that there is only one possible value from $customer. THIS IS NOT TRUE! My mistake.

        foreach $customer(keys %myHash){ $money_spent_9am = $myHash{$customer}{money9am}; $money_spent_3pm = $myHash{$customer}{money3pm}; }
        This is how I have it now, Ill run some tests if this is what I want.