Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I have this text file similar to the one here that I am trying to find how many items are there for each account number ( position 2 in the array for account number) in adding its total ( position 7 in the array), I am checking for duplicates, but that part I got it, but getting lost on the best way to get the items counted and the total from each account followed by the grand total.
Thanks for looking!
#!/usr/bin/perl use strict; my %seen=(); my %count; my @flds; my @g_total; while (<DATA>){ chomp; # get rid of dup lines next if $seen{$_}++; @flds=split /,/; # look up accounts in array position 2 and count its accuracies and a +dd amounts from position arrays 7 fro each one. if (!$seen{$flds[2]}++){ if($flds[2] eq $flds[2]){ $flds[7]+=$flds[7]; print "From Account# $flds[2] - Total = $flds[7]\n"; } push @g_total, $flds[7]+=$flds[7]; } print "\nGrand Total = $g_total[0]\n"; __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
  • Comment on Counting how many times item is occurring and adding the total from array help!
  • Download Code

Replies are listed 'Best First'.
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
    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
      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
Re: Counting how many times item is occurring and adding the total from array help!
by toolic (Bishop) on Sep 14, 2012 at 18:19 UTC
    I get a compile error. I added a "}" before your last print. Is that where it should go?

    I get a warning message with warnings enabled. Try to clean that up.

    if($flds[2] eq $flds[2]){
    Comparing something to itself is probably not what you meant.
      Missed the "}",sorry. Thats where one of my issue is, how to add the values if the account on position 2 are equal.