Discuss Total counter != sum of individual parts? in the Perl Programming forum on Dev Shed. Total counter != sum of individual parts? Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
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?
Posts: 6,894
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 36 m 34 sec
Reputation Power: 3885
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.
Posts: 507
Time spent in forums: 4 Days 19 h 10 m 26 sec
Reputation Power: 385
Quote:
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?
Quote:
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.
Quote:
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.
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...
Quote:
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
Posts: 6,894
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 36 m 34 sec
Reputation Power: 3885
Quote:
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.
Quote:
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);
Posts: 1,645
Time spent in forums: 1 Month 2 Weeks 1 Day 22 h 51 m 15 sec
Reputation Power: 1170
Quote:
$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?
Posts: 320
Time spent in forums: 3 Days 16 h 57 m 54 sec
Reputation Power: 227
Fishmonger ->
Quote:
$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?