#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    335
    Rep Power
    246

    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
    Free MP3 Dance Music Downloads

    To err is human; To really balls things up you need Microsoft!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    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'})
    would add possibly to

    Code:
    $recs[0]{'CompVal'}
    but there is nothing in your code which would lead to think that it should add to ComVal.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    335
    Rep Power
    246
    $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?
    Free MP3 Dance Music Downloads

    To err is human; To really balls things up you need Microsoft!
  6. #4
  7. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    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.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    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.
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    335
    Rep Power
    246
    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 = { 'Value' => '145.0000', 'Cases' => '1', 'Costs' => '.0000', 'Adv_MemNo' => 'Mxxxx-0xx', 'Type' => 'SUB', 'Adviser' => 'David xxxx', 'Fees' => '.0000', 'Category' => 'GI' }; $VAR2 = { 'Value' => '800.0000', 'Cases' => '1', 'Costs' => '.0000', 'Adv_MemNo' => 'Mxxxx-0xx', 'Type' => 'SUB', 'Adviser' => 'David xxxx', 'Fees' => '.0000', 'Category' => 'Non-Reg Mortgage' }; $VAR3 = { 'Value' => '2082.0000', 'Cases' => '2', 'Costs' => '.0000', 'Adv_MemNo' => 'Mxxxx-0xx', 'Type' => 'SUB', 'Adviser' => 'David xxxx', 'Fees' => '.0000', 'Category' => 'Protection' }; $VAR4 = { 'Value' => '4527.0000', 'Cases' => '6', 'Costs' => '.0000', 'Adv_MemNo' => 'Mxxxx-0xx', 'Type' => 'SUB', 'Adviser' => 'David xxxx', 'Fees' => '297.0000', 'Category' => 'Reg Mortgage' }; $VAR5 = { 'Value' => '2033.0300', 'Cases' => '17', 'Costs' => '.0000', 'Adv_MemNo' => 'Mxxxx-0xx', 'Type' => 'COMP', 'Adviser' => 'David xxxx', 'Fees' => '.0000', 'Category' => 'GI' }; $VAR6 = { 'Value' => '3150.0000', 'Cases' => '5', 'Costs' => '.0000', 'Adv_MemNo' => 'Mxxxx-0xx', 'Type' => 'COMP', 'Adviser' => 'David xxxx', 'Fees' => '.0000', 'Category' => 'Non-Reg Mortgage' }; $VAR7 = { 'Value' => '2666.0000', 'Cases' => '8', 'Costs' => '.0000', 'Adv_MemNo' => 'Mxxxx-0xx', 'Type' => 'COMP', 'Adviser' => 'David xxxx', 'Fees' => '.0000', 'Category' => 'Protection' }; $VAR8 = { 'Value' => '31833.7000', 'Cases' => '59', 'Costs' => '.0000', 'Adv_MemNo' => 'Mxxxx-0xx', 'Type' => 'COMP', 'Adviser' => 'David xxxx', 'Fees' => '2773.0000', 'Category' => 'Reg Mortgage' };
    --------------------------------------------------------------------------------
    $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
            if(!@recs || $recs[@recs-1]{'Adviser'} ne $_->{'Adviser'})
            {
                # set start / leave dates
                $mdate{$_->{'Adv_MemNo'}} = substr($mdate{$_->{'Adv_MemNo'}},0,10);
                if($ldate{$_->{'Adv_MemNo'}} eq ""){$ldate{$_->{'Adv_MemNo'}} = "*";}
                else{ 
                    $ldate{$_->{'Adv_MemNo'}} = substr($ldate{$_->{'Adv_MemNo'}},0,10);
                    my @ldate = split(/-/, $ldate{$_->{'Adv_MemNo'}});
                    $ldate{$_->{'Adv_MemNo'}} = $ldate[2] . "/" .  $ldate[1] . "/" .  $ldate[0];  
                }
                my @mdate = split(/-/, $mdate{$_->{'Adv_MemNo'}});
                $mdate{$_->{'Adv_MemNo'}} = $mdate[2] . "/" .  $mdate[1] . "/" .  $mdate[0];
              
                push (@recs,{'Adv_MemNo' => $_->{'Adv_MemNo'},'Adviser' => $_->{'Adviser'},'CompanyName' => $_->{'CompanyName'},'Join' => $mdate{$_->{'Adv_MemNo'}},'Leave' => $ldate{$_->{'Adv_MemNo'}}});         
                $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'};                
                }
      
            }
            
        }
    
        # sort results by Adviser 
        @recs = sort { $a->{'Adviser'} cmp $b->{'Adviser'} } @recs; 
        
        
        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.
    Free MP3 Dance Music Downloads

    To err is human; To really balls things up you need Microsoft!
  12. #7
  13. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    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);
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,930
    Rep Power
    1225
    $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?
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    335
    Rep Power
    246
    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 )

    Comments on this post

    • ishnid agrees : Glad you got it sorted out.
    Last edited by 1DMF; October 30th, 2012 at 02:23 PM.
    Free MP3 Dance Music Downloads

    To err is human; To really balls things up you need Microsoft!

IMN logo majestic logo threadwatch logo seochat tools logo