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

Gurus: I trying to figure out what is the best way to sum up a set of records that's in array. Should I reset the variables after it loops through to the next record? If so, how? Now, I'll need group the items that I want to generate individual PDFs for. I was thinking about dumping it out into a text file or database, so I can run a SQL query to sum up each category. Your help will be appreciated.
#!/usr/bin/perl -w use strict; use warnings; open PM, "C:/REPORT" or die "Couldn't open Report file: $!"; my (@fields,$systime,$company,$company_prev,$batch_num,$effent_date,$e +ntry_desc,$date_crt,$load_num,$eofbatch,$credits,$debits,$comp_ident, $total, $total_items); $batch_num = 0; while (my $pimrpt = <PM>) { next if ($pimrpt =~ m/^(\s)*$/ || $pimrpt =~ m/^(\*)/ || substr($p +imrpt,9,10) =~ m/SETTLEMENT/ || substr($pimrpt,12,6) =~ m/NUMBER/ || substr($pimrpt,8,9) =~ m/\d{9}/); #Skips over blank lines +. next unless (substr($pimrpt, 0, 8) =~ m/COMPANY/ || substr($pimrpt, 27, 12) =~ m/BATCH NUMBER/ || substr($pimrpt, 51, 12) =~ m/DATE CREATED/ || substr($pimrpt, 79, 17) =~ m/ENTRY DESCRIPTION/ || substr($pimrpt,0,7) eq $batch_num); if (substr($pimrpt, 0, 8) =~ m/COMPANY/) { $company = substr($pimrpt, 9, 18); }; if (substr($pimrpt, 27, 12) =~ m/BATCH NUMBER/) { $batch_num = substr($pimrpt, 42, 7); $effent_date = substr($pimrpt, 69, 8); $comp_ident = substr($pimrpt, 102, 9); }; if (substr($pimrpt, 79, 17) =~ m/ENTRY DESCRIPTION/) { $entry_desc = substr($pimrpt, 101, 13); }; if (substr($pimrpt, 51, 12) =~ m/DATE CREATED/) { $date_crt = substr($pimrpt, 69, 8); $load_num = substr($pimrpt, 107, 4) }; if (substr($pimrpt,0,7) eq $batch_num) { $total_items = substr($pimrpt, 8,16); $debits = substr($pimrpt, 24, 20); $credits = substr($pimrpt, 44, 20); push @fields, {company_ident => $comp_ident, company => $company, batch_number => $batch_num, effective_date => $effent_date, entry_description => $entry_desc, date_created => $date_crt, load_number => $load_num, debits => $debits, credits => $credits}; }; }; foreach my $x (@fields) { print $x->{company_ident} . "\t" . $x->{company} . "\t" . $x->{b +atch_number} . "\t" . $x->{effective_date} . "\t" . $x->{date_created} . "\t" . $x->{load_number} . "\t" . $x- +>{debits} . "\t" . $x->{credits} . "\n"; } print "Press Return to Conintue..."; my $x = <STDIN>;

Replies are listed 'Best First'.
Re: Sum fields in Array
by jethro (Monsignor) on Feb 04, 2010 at 20:26 UTC

    Probably I'm misunderstanding something about your question, but summing up is simply done like this:

    ... my $sumcredits; while (my $pimrpt = <PM>) { ... $sumcredits+= $credits; ... } print "The sum of credits is $sumcredits\n";

    If you need to add credits seperate for each company, change that to

    ... my %sumcredits; while (my $pimrpt = <PM>) { ... $sumcredits{$company}+= $credits; ... } foreach my $comp (keys %sumcredits) { print "The sum of credits for $comp is $sumcredits{$comp}\n"; }
      Hey Jethro, I tried what you suggested and it worked. I used the 2nd part of you suggested. Is the code ( $sumcredits{$company}+= $credits;) the same as a group by in SQL? Thanks for you help again because I thought it was going to be a real pain to do this. Dave
        Here's another question for you. How can I take out a comma in a money field? It doesn't like it when it adds them up.
Re: Sum fields in Array
by hangon (Deacon) on Feb 04, 2010 at 21:07 UTC
    Should I reset the variables after it loops through to the next record? If so, how?

    It's not clear what you're really trying to acomplish, but I don't see anything being summed. Perhaps you could provide some sample input and expected output.

    As far as clearing variables, you should declare them inside the while loop so they are in the smallest scope possible. Declaring the variables at the beginning like you have done essentially makes them all globals.

    my @fields; while (my $pimrpt = <PM>) { my ($systime,$company, $company_prev, ... ); ... etc
      Good suggestion...I've done that. I am going through a report and pulling various values. My question is when I have is when I hit another group of values that I need to pull from. Should I clear the previous set first by assigning the variables to null. Also should I push the variables into array or wait till have all the variables set and then push them into an array.