in reply to Re^2: Running Total Array or Hash
in thread Running Total Array or Hash

Hello, I guess I have 1 additional component. Assuming I display everything properly can I manipulate the total with a math formula and put it back in the array prior to printing it?
for my $key (sort keys %client_total) { $client_total = {$key} = $client_total/10; } for my $key (sort keys %client_total) { print "$key: $client_total{$key}\n"; }
I'm not clear on on how to manipulate the value, the above gives me an error. Thanks!!

Replies are listed 'Best First'.
Re^4: Running Total Array or Hash
by VincentK (Beadle) on Mar 10, 2014 at 20:21 UTC
    Hello DeWebDude

    You are using a hash, not an array. In order to access the value for each key, your code needs to look something like this.

    I hope this helps.

    Vincent.

    for my $key (sort keys %client_total) { $client_total{$key} = ($client_total{$key}) / 10; print "$key: $client_total{$key}\n"; }
Re^3: Running Total Array or Hash
by Laurent_R (Canon) on Mar 10, 2014 at 21:41 UTC

    This code:

    for my $key (sort keys %client_total) { $client_total = {$key} = $client_total/10; }
    does not make much sense to me. Perhaps you want something like this:
    for my $key (sort keys %client_total) { $client_total{$key} = $client_total{$key}/10; }
    Although, if this is what you want, there is no need to sort the keys for the calculation.

    This could be done in a more concise (but less explicit) way:

    $_/=10 for values %client_total;
    Or even possibly:
    map {$_/=10} values %client_total;
    (Although I really prefer the previous solution.) Please also note that you posted this message twice and you got an answer from VincentK (look above), which might be what you are looking for.

Re^4: Running Total Array or Hash
by kcott (Archbishop) on Mar 11, 2014 at 07:09 UTC

    Looking at "$client_total = {$key} = $client_total/10;", I recommend you read "perlintro -- a brief introduction and overview of Perl".

    Here's how you might go about extracting data from a file, storing that data in a data structure, performing calculations on that data, and subsequently storing the results in the same data structure. I've reused the data, and a small amount of the code, from ++kennethk's earlier post. This is intended to give you some pointers as I've really no idea what you're actually trying to achieve.

    #!/usr/bin/env perl use strict; use warnings; use List::Util qw{sum}; my %data = (client => {}, all => {}); my $client = $data{client}; my $all = $data{all}; while (<DATA>) { my ($name, $amount) = split; push @{$client->{$name}{amounts}}, $amount; } for (sort keys %$client) { push @{$all->{clients}}, $_; $client->{$_}{total} = sum @{$client->{$_}{amounts}}; $all->{total} += $client->{$_}{total}; } for (keys %$client) { $client->{$_}{percent} = $client->{$_}{total} * 100 / $all->{total +}; } use Data::Dump; dd \%data; __DATA__ a 1 b 5 a 3 b 7 c 9 a 2

    Output:

    { all => { clients => ["a", "b", "c"], total => 27 }, client => { a => { amounts => [1, 3, 2], percent => 22.2222222222222, total => + 6 }, b => { amounts => [5, 7], percent => 44.4444444444444, total => 12 + }, c => { amounts => [9], percent => 33.3333333333333, total => 9 }, }, }

    -- Ken