### Thread: Total counter != sum of individual parts?

1. #### Total counter != sum of individual parts?

Hi,

I have a report that uses the following..

Code:
```            if(\$_->{'Category'} eq 'GI'){
\$recs[@recs-1]{'GComp'} = \$_->{'Cases'};
\$recs[@recs-1]{'GCompVal'} = (\$_->{'Value'} + \$_->{'Fees'}) - \$_->{'Costs'};
\$recs[@recs-1]{'CompVal'} += \$recs[@recs-1]{'GCompVal'};
}
elsif(\$_->{'Category'} eq 'Protection'){
\$recs[@recs-1]{'PComp'} = \$_->{'Cases'};
\$recs[@recs-1]{'PCompVal'} = (\$_->{'Value'} + \$_->{'Fees'}) - \$_->{'Costs'};
\$recs[@recs-1]{'CompVal'} += \$recs[@recs-1]{'PCompVal'};
}
else{
\$recs[@recs-1]{'MComp'} = \$_->{'Cases'};
\$recs[@recs-1]{'MCompVal'} = (\$_->{'Value'} + \$_->{'Fees'}) - \$_->{'Costs'};
\$recs[@recs-1]{'CompVal'} += \$recs[@recs-1]{'MCompVal'};
}```
What I cannot understand is that if I print the following....
Code:
`(\$recs[0]{'MCompVal'} + \$recs[0]{'PCompVal'} + \$recs[0]{'GCompVal'})`
it does not add up to 'CompVal'?

How is this happening, the code that calculates 'CompVal' is the same place where the individual sums are calculated 'GCompVal' , 'PCompVal', 'MCompVal' , so therefore 'GCompVal' + 'PCompVal' + 'MCompVal' should = 'CompVal' but it doesn't?

What on earth is going on?

Thanks,
1DMF
2. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
Jun 2012
Location
Paris area, France
Posts
846
Rep Power
499
Originally Posted by 1DMF
What I cannot understand is that if I print the following....
Code:
`(\$recs[0]{'MCompVal'} + \$recs[0]{'PCompVal'} + \$recs[0]{'GCompVal'})`
it does not add up to 'CompVal'?
Is it supposed to be the case?

I would think that

Code:
`(\$recs[0]{'MCompVal'} + \$recs[0]{'PCompVal'} + \$recs[0]{'GCompVal'})`

Code:
`\$recs[0]{'CompVal'}`
but there is nothing in your code which would lead to think that it should add to ComVal.
3. \$recs[@recs-1] = \$recs[0]

They are the same thing as it is a single indexed array with a hash in it.

CompVal is a running total , which should equal the sum of the other 3 totals but it doesn't and I don't undertsand why.

Could it be a rounding thing or a float issue ?

Though it is out by nearly £2,000.00?
4. Hard to know for sure without seeing the code in context.

From what you've posted, I'd check that some of those values aren't being set twice. Any time a GCompVal, PCompVal or MCompVal is set, any previous value in those is overwritten, whereas the CompVal is incremented each time. If your code sets a GCompVal (for example) twice, then only the second value will be store in GCompVal whereas CompVal will have added both values.
5. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
Jun 2012
Location
Paris area, France
Posts
846
Rep Power
499
Originally Posted by 1DMF
\$recs[@recs-1] = \$recs[0]
That bit, I understood from your code and comments. But why do you need an array if subscript is always 0?

CompVal is a running total , which should equal the sum of the other 3 totals but it doesn't and I don't undertsand why.
'CompVa'l is a litteral representing a string of characters, not a numeric total. 'CompVa'l is bound not to be equal to you sum. Please indicate clearly what is not equal to you sum of three values.

Could it be a rounding thing or a float issue ?

Though it is out by nearly £2,000.00?
I don't think so.

Use the debugger, or print the values of your variables before and after the piece of code you posted, or post more of your code. From what you posted, we do not have enough information. The problem, if any, is probably somewhere else.
6. Hey ishnid, how the devil are you? Long time no speak

Should we call you Professor from now on

Laurent, not sure why you believe the total is a string literal, but anyway, here is the output from data dumper.

The first is @subs, the second is @recs.

--------------------------------------------------------------------------------
\$VAR1 = { 'GCompVal' => '2033.03', 'PSub' => '2', 'SubVal' => 7851, 'PSubVal' => 2082, 'Leave' => '*', 'Adv_MemNo' => 'Mxxxx-0xx', 'Join' => '31/10/2011', 'GComp' => '17', 'PCompVal' => 2666, 'MSubVal' => 4824, 'MCompVal' => '34606.7', 'MComp' => '59', 'CompVal' => '42455.73', 'Adviser' => 'David xxxx', 'MSub' => '6', 'CompanyName' => undef, 'GSub' => '1', 'GSubVal' => 145, 'PComp' => '8' };
here is the code...
Code:
```#loop and build data
for(@subs){

# check if next broker
{
# set start / leave dates
else{
\$ldate{\$_->{'Adv_MemNo'}} = \$ldate[2] . "/" .  \$ldate[1] . "/" .  \$ldate[0];
}
\$mdate{\$_->{'Adv_MemNo'}} = \$mdate[2] . "/" .  \$mdate[1] . "/" .  \$mdate[0];

\$recs[@recs-1]{'MSub'} = 0;
\$recs[@recs-1]{'MSubVal'} = 0;
\$recs[@recs-1]{'MComp'} = 0;
\$recs[@recs-1]{'MCompVal'} = 0;
\$recs[@recs-1]{'GSub'} = 0;
\$recs[@recs-1]{'GSubVal'} = 0;
\$recs[@recs-1]{'GComp'} = 0;
\$recs[@recs-1]{'GCompVal'} = 0;
\$recs[@recs-1]{'PSub'} = 0;
\$recs[@recs-1]{'PSubVal'} = 0;
\$recs[@recs-1]{'PComp'} = 0;
\$recs[@recs-1]{'PCompVal'} = 0;
\$recs[@recs-1]{'SubVal'} = 0;
\$recs[@recs-1]{'CompVal'} = 0;
}

if(\$_->{'Type'} eq 'SUB'){

if(\$_->{'Category'} eq 'GI'){
\$recs[@recs-1]{'GSub'} = \$_->{'Cases'};
\$recs[@recs-1]{'GSubVal'} = (\$_->{'Value'} + \$_->{'Fees'}) - \$_->{'Costs'};
}
elsif(\$_->{'Category'} eq 'Protection'){
\$recs[@recs-1]{'PSub'} = \$_->{'Cases'};
\$recs[@recs-1]{'PSubVal'} = (\$_->{'Value'} + \$_->{'Fees'}) - \$_->{'Costs'};
}
else{
\$recs[@recs-1]{'MSub'} = \$_->{'Cases'};
\$recs[@recs-1]{'MSubVal'} = (\$_->{'Value'} + \$_->{'Fees'}) - \$_->{'Costs'};
}

\$recs[@recs-1]{'SubVal'} +=  ((\$_->{'Value'} + \$_->{'Fees'}) - \$_->{'Costs'});

}
else{

if(\$_->{'Category'} eq 'GI'){
\$recs[@recs-1]{'GComp'} = \$_->{'Cases'};
\$recs[@recs-1]{'GCompVal'} = (\$_->{'Value'} + \$_->{'Fees'}) - \$_->{'Costs'};
\$recs[@recs-1]{'CompVal'} += \$recs[@recs-1]{'GCompVal'};
}
elsif(\$_->{'Category'} eq 'Protection'){
\$recs[@recs-1]{'PComp'} = \$_->{'Cases'};
\$recs[@recs-1]{'PCompVal'} = (\$_->{'Value'} + \$_->{'Fees'}) - \$_->{'Costs'};
\$recs[@recs-1]{'CompVal'} += \$recs[@recs-1]{'PCompVal'};
}
else{
\$recs[@recs-1]{'MComp'} = \$_->{'Cases'};
\$recs[@recs-1]{'MCompVal'} = (\$_->{'Value'} + \$_->{'Fees'}) - \$_->{'Costs'};
\$recs[@recs-1]{'CompVal'} += \$recs[@recs-1]{'MCompVal'};
}

}

}

use Data::Dumper;
print '<!--T--> <hr>' . Dumper(@subs) . '<hr>' . Dumper(@recs)```
You will see that the total for CompVal = 42455.73 but the sum of the other 3 totals = 39305.73 ?

I've even moved the 'Comp' total counter to inside the 3 if statements to be sure that when a value is added to either of the sub totals, it also includes the grand total, you will see how it was originally for the subs, as I have left that counter as is. Which incidently is out by exactly 800?

I did a simple output of the records as they were looped in @subs...
Value = 145.0000, Fees = .0000 , Costs = .0000 , Type = SUB, Cat = GI

Value = 800.0000, Fees = .0000 , Costs = .0000 , Type = SUB, Cat = Non-Reg Mortgage

Value = 2082.0000, Fees = .0000 , Costs = .0000 , Type = SUB, Cat = Protection

Value = 4527.0000, Fees = 297.0000 , Costs = .0000 , Type = SUB, Cat = Reg Mortgage

Value = 2033.0300, Fees = .0000 , Costs = .0000 , Type = COMP, Cat = GI

Value = 3150.0000, Fees = .0000 , Costs = .0000 , Type = COMP, Cat = Non-Reg Mortgage

Value = 2666.0000, Fees = .0000 , Costs = .0000 , Type = COMP, Cat = Protection

Value = 31833.7000, Fees = 2773.0000 , Costs = .0000 , Type = COMP, Cat = Reg Mortgage
using
Code:
`        print "<p>Value = \$_->{'Value'}, Fees = \$_->{'Fees'}) , Costs =  \$_->{'Costs'} , Type =  \$_->{'Type'}, Cat = \$_->{'Category'}</p>";`
And if you add up the 4 'Sub' record values you get 7554, then minus the 297 fee = 7257, yet the SubVal = 7851.

None of the figures are adding up, why?
Last edited by 1DMF; October 30th, 2012 at 05:29 AM.
7. Originally Posted by 1DMF
Hey ishnid, how the devil are you? Long time no speak

Should we call you Professor from now on
Not yet, unfortunately. Hopefully, 'doctor' will apply by January though! Hope you're keeping well.

Originally Posted by 1DMF
And if you add up the 4 'Sub' record values you get 7554, then minus the 297 fee = 7257, yet the SubVal = 7851.
I think this is the key. I suspect it's not a coincidence that 7554+297=7851 but what you want is 7554-297=7257. It looks like you're adding on the fees instead of subtracting them. Could that be the issue, or am I misreading?

As an aside, when posting Data:umper output, it's easier for someone like me to do something with the data if you dump a reference to the array, i.e. print Dumper(\@recs);
8. No Profile Picture
Contributing User
Devshed Regular (2000 - 2499 posts)

Join Date
Apr 2009
Posts
2,232
Rep Power
1297
\$recs[@recs-1] = \$recs[0]

They are the same thing ...
No, they are not.

When using @recs-1 as the array index, which BTW is a very poor coding practice, you're specifying the last element of the array, not the first. If the array only contains a single element then, yes, 0 and @recs-1 refer to the same element, but you shouldn't confuse the issue by using that syntax.

If you want to refer to the last element, then the proper syntax would be:
Code:
`\$recs[-1]`
So, in your posted code snippets, did you really want the last element of @recs, or the first element?

Currently, @recs ends up with only 1 element. Will that always be the case? If so, then why use an array?
9. Fishmonger ->
\$recs[-1]
didn't know that many thanks. (I've updated the code)

And of course there isn't always 1 , hence the array!

ishnd ->

cool well done and congratulations Dr. Isnid :-)

as to the total ...hmm, I've confused myslef here!

The sub total is meant to be 7851, yes it's (value + fees) - costs

sorry, but the point is the output shows subval as 7,851 but each group total (MSubVal + GSubVal + PSubVal) = 7051? instead of 7851 ? 800 short, which is interesting because that's exactly the value of the case (Non-Reg Mortgage) 800?

I've checked and @recs is size 1, where is this value going astray?

How are the grand totals correct but the sum of the parts don't equal the total?

Edit -> finally the penny drops!

It should be...

Code:
```               if(\$case->{'Status'} eq 'SUB'){

if(\$case->{'Category'} eq 'GI'){
\$recs[-1]{'GSub'} += \$case->{'Cases'};
\$recs[-1]{'GSubVal'} += ((\$case->{'Value'} + \$case->{'Fees'}) - \$case->{'Costs'});
}
elsif(\$case->{'Category'} eq 'Protection'){
\$recs[-1]{'PSub'} += \$case->{'Cases'};
\$recs[-1]{'PSubVal'} += ((\$case->{'Value'} + \$case->{'Fees'}) - \$case->{'Costs'});
}
else{
\$recs[-1]{'MSub'} += \$case->{'Cases'};
\$recs[-1]{'MSubVal'} += ((\$case->{'Value'} + \$case->{'Fees'}) - \$case->{'Costs'});
}

\$recs[-1]{'SubVal'} += ((\$case->{'Value'} + \$case->{'Fees'}) - \$case->{'Costs'});
}
else{

if(\$case->{'Category'} eq 'GI'){
\$recs[-1]{'GComp'} += \$case->{'Cases'};
\$recs[-1]{'GCompVal'} += ((\$case->{'Value'} + \$case->{'Fees'}) - \$case->{'Costs'});
}
elsif(\$case->{'Category'} eq 'Protection'){
\$recs[-1]{'PComp'} += \$case->{'Cases'};
\$recs[-1]{'PCompVal'} += ((\$case->{'Value'} + \$case->{'Fees'}) - \$case->{'Costs'});
}
else{
\$recs[-1]{'MComp'} += \$case->{'Cases'};
\$recs[-1]{'MCompVal'} += ((\$case->{'Value'} + \$case->{'Fees'}) - \$case->{'Costs'});
}

\$recs[-1]{'CompVal'} += ((\$case->{'Value'} + \$case->{'Fees'}) - \$case->{'Costs'});
}```
Man, roll on Christmas, coz I clearly need a break!

(Fishmonger - you'll notice I tidied up the code for you )