Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Running Total Array or Hash

by kennethk (Monsignor)
on Mar 10, 2014 at 16:52 UTC ( #1077721=note: print w/ replies, xml ) Need Help??


in reply to Running Total Array or Hash

As others have pointed out, you are light on specifics. The more specific you are, the more helpful we can be. This includes sample input, scrubbed as necessary and wrapped in code tags; desired output, wrapped in code tags; and attempted code or at least a description of your algorithm. See How do I post a question effectively?.

each record has the name of the client
This sounds like indexing results by strings, which says "use a hash" to me. Algorithmically, that might look something like:
#!/usr/bin/perl -w use strict; my %sum; while (<DATA>) { my ($name, $value) = split; $sum{$name} += $value; } for my $key (sort keys %sum) { print "$key: $sum{$key}\n"; } __DATA__ a 1 b 5 a 3 b 7 c 9 a 2

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


Comment on Re: Running Total Array or Hash
Download Code
Re^2: Running Total Array or Hash
by DeWebDude (Initiate) on Mar 10, 2014 at 19:49 UTC
    Hello All, Thank you KennethK, This is exactly what I needed, just enough to give me direction. Worked like a charm!
      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!!
        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"; }

        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.

        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

Reaped: Re^2: Running Total Array or Hash
by NodeReaper (Curate) on Mar 10, 2014 at 20:46 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2014-07-26 14:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (177 votes), past polls