PDA

View Full Version : a little help with this hash plz.


bazz
09-16-2008, 04:14 AM
Hi,

I must be losing the plot here because I can't seem to get this hash to buiild correctly and I can't work it out in the past two hours.

please help

the query (with the retrieved data outputted in full and th hash output in the wrong order and duplicated.


while ( my $room_rate = $get_individual_room_rates->fetchrow_hashref)
{
my $room_rate_id = $room_rate->{room_rate_id};
my $tariffs_data = $room_rate->{tariffs_data};
my @tariffs_data = (split /,/, $tariffs_data);

my ($date_range_type, $period_start, $period_end, $day_start, $price, $room_type) = split /\//, $tariffs_data[0], 7;

my $summary = join ":", $room_type, $price;
$hash{$room_type}{$price}=$summary;
#$hash{$room_type}{$price}++;

print qq(
rt=$room_type : p=$price <br />
); has all the required data


}

foreach my $key(sort keys %hash ) {
print qq( key=$key );
foreach my $value (sort keys % { $hash{$key}}) {
print qq( :: val=$value );
}
} seems to output OK but for the dumper concern

print Dumper \%hash;
return;



here's is what is being dumped. the forst poart is in the correct order but it gets messed up in the poorly compiled hash.


rt=Double En-Suite : p=102 <br />

rt=Double En-Suite : p=93 <br />

rt=Luxury Suite : p=99 <br />

rt=Luxury Suite : p=344 <br />


rt=Luxury Suite : p=125 <br />

rt=Luxury Suite : p=155 <br />

rt=Luxury Suite : p=165 <br />

rt=Luxury Suite : p=205 <br />

rt=Luxury Suite : p=335 <br />

rt=Luxury Suite : p=325 <br />


rt=Standard Room : p=65.5 <br />

rt=Standard Room : p=60 <br />

rt=Standard Room : p=110 <br />
key=Double En-Suite :: val=102 :: val=93 key=Luxury Suite :: val=125 :: val=155 :: val=165 :: val=205 :: val=325 :: val=335 :: val=344 :: val=99 key=Standard Room :: val=110 :: val=60 :: val=65.5 $VAR1 = {
'Standard Room' => {
'60' => 'Standard Room:60',
'65.5' => 'Standard Room:65.5',
'110' => 'Standard Room:110'
},
'Luxury Suite' => {
'205' => 'Luxury Suite:205',
'155' => 'Luxury Suite:155',
'99' => 'Luxury Suite:99',
'165' => 'Luxury Suite:165',
'344' => 'Luxury Suite:344',
'335' => 'Luxury Suite:335',
'125' => 'Luxury Suite:125',
'325' => 'Luxury Suite:325'
},
'Double En-Suite' => {
'93' => 'Double En-Suite:93',
'102' => 'Double En-Suite:102'
}
};





hashes and I just don't seem to get on together. :(

bazz

KevinADC
09-16-2008, 05:48 AM
if the hash keys are numbers and you wanted them sorted numerically you have to use <=>:

(sort {$a <=> $b} keys %hash )