Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: count troubles.

  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    count troubles.

    Hi,

    I have built a hash as shown below. I did this because within a while loop, I couldn't seem to make the outputted sub_totals, add on each iteration without only getting the total of the last loop.

    anyhow, even when looping through the while loop for the hash, I can't add up the values correctly.
    Code:
    'subs' => {
              '97' => {
                     '90.00' => 1,
                     '76.50' => 1,
                     '90.90' => 1
                        }
              },
    Code:
      my $room_total=0;
      my $sub_total=0;
    
      while (my ($tariff, $sub_total) = each %room_total) {
    
       $room_total = ($sub_total + $sub_total);   
      }
      $room_total = sprintf("%.2f", $room_total );
    
      print qq( <tr><td>total=$room_total</td></tr> );
    that hash is totalled as 350533824.00

    I think the way a while loop runs, being different from a foreach, is making me confuse myself.

    any pointers very welcome

    oh, just thought; maybe I should process the hash with a foreach loop?

    bazz
    Last edited by bazz; 09-17-2009 at 06:27 AM.
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #2
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    nope. this just gives me 180.80 as a total instead of 257.40

    Code:
    my $room_total=0;
      my $sub=0;
      foreach my $tariff (keys %room_total) {
        foreach $sub ( keys % {$room_total{$tariff} } ) {
     
         $room_total = ($sub + $sub);   
        }
      } 
     
      $room_total = sprintf("%.2f", $room_total );
              print qq( <tr><td>total=$room_total</td></tr> );
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #3
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Blimey, I got it with an array.

    Code:
    my $sub_total = 0;
    ($sub_total+=$_) for @subs;
    print qq( <tr><td class='left_side'><p>Sub Total:</p></td><td>$sub_total</td></tr> );
    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •