in reply to Sum fields in Array

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"; }

Replies are listed 'Best First'.
Re^2: Sum fields in Array
by drodinthe559 (Monk) on Feb 04, 2010 at 20:56 UTC
    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.

        Yes, that is similar to SQLs group by.

        Do you mean commas that are used as decimal points (done in some parts of the world, for example Germany), or commas as separators for every 10^3 step?

        In the first case you might substitute the comma with a decimal point ( $num=~s/,/./;), in the second case you might remove all the commas before counting ( $num=~s/,//g; the g means global, do it to all commas, not just one)