...

View Full Version : Resolved count troubles.



bazz
09-17-2009, 07:04 AM
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.


'subs' => {
'97' => {
'90.00' => 1,
'76.50' => 1,
'90.90' => 1
}
},




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 :eek:

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

bazz
09-17-2009, 07:11 AM
nope. this just gives me 180.80 as a total instead of 257.40



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> );



:(

bazz
09-17-2009, 07:27 AM
Blimey, I got it with an array.



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum