Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Counting how many times item is occurring and adding the total from array help!

by CountZero (Bishop)
on Sep 14, 2012 at 19:34 UTC ( #993784=note: print w/replies, xml ) Need Help??


in reply to Counting how many times item is occurring and adding the total from array help!

Is this doing what you want?
use Modern::Perl; use Data::Dump qw/dump/; my %seen; my %accounts; my $g_total; while (<DATA>) { next if $seen{$_}++; my ( $account_number, $amount ) = ( split /,/ )[ 2, 7 ]; @{ $accounts{$account_number} }[0] += $amount; @{ $accounts{$account_number} }[1]++; $g_total += $amount; } say dump( \%accounts ); say "Grand Total = $g_total"; __DATA__ Joe Smith,12345678,44552,02/11/2011,no email, MA,USA,900.00,updated Joe Smith,12345678,44552,02/11/2011,no email, MA,USA,900.00,updated Joe Smith,12345678,44552,02/11/2011,no email, MA,USA,900.00,updated Cindy Abbot,122233678,01122,08/09/2012,test@tok.com, CA,EUR,-120.00,up +dated Mary Lou,33456678,44552,01/11/2011,no email, MA,USA,400.00,updated SMith Doo,12345678,44592,02/11/2012,test@test.com, MA,USA,100.00,updat +ed Mario Att,00056789,022345,03/10/2010,no email, MA,USA,40.00,outdated Mario Att,00056789,022345,03/10/2010,no email, MA,USA,40.00,outdated Mario Att,00056789,022345,03/10/2010,ok@ok.com, MA,USA,40.00,outdated Maria Smither,12345678,00051,02/11/2011,no email, MA,USA,750.00,outdat +ed Dan Smither,12345678,00051,02/11/2011,no email, MA,USA,250.00,outdated

Output:

{ "00051" => [1000, 2], "01122" => [-120, 1], "022345" => [80, 2], "44552" => [1300, 2], "44592" => [100, 1], } Grand Total = 2360

The hash %accounts hold the accountnumber as key and each value is a reference to an array containing the total for that account and the number of times the accountnumber was found in the data.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^2: Counting how many times item is occurring and adding the total from array help!
by Anonymous Monk on Sep 14, 2012 at 19:42 UTC
    This is what I am tryin to get at the end:

    Account 44552 - ccurring 1 time(s) - Total = 900.00
    Account 00051O - ccurring 2 time(s) - Total = 1,000.00
    Account 01122 - ccurring 2 time(s) - Total = -120.00
    ...

    Grand Total = 1,780.00

    Thanks for helping!
      Easy! replace the last two lines by:
      say "Account $_ - occurring $accounts{$_}->[1] time(s) - Total = $acco +unts{$_}->[0]" for keys %accounts; say "Grand Total = $g_total";

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics
        Great help, thanks a lot!!!! On my way to code some more now!!!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://993784]
help
Chatterbox?
[Lady_Aleena]: james28909, you might want to look at the other two on my scratchpad.
[james28909]: after you send to a sub, you can check if it is array or ref ect with ref
[james28909]: check if it is a hash or an array ect with ref in one sub.
[james28909]: like all the subs calling get_data get_array get_ect, you could just use get_data. once you send the data to get_data, check if it is a hash or an array ect. and do functions for each, in one sub
[james28909]: that looks like it would be alot more work than just renaming the sub though lol
[Lady_Aleena]: james28909, did you even look at the subs?

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2017-05-24 04:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?