PDA

View Full Version : getting third hash value to merge with another hash.


bazz
08-08-2008, 09:43 PM
Hi,

I have two hashes. One is taken from the session, whereas the other is a validity-checker hash, taken from the db.

chosen options hash: taken from session dump

'options' => {
'Pillows' => 'Goose_Down_Pillow',
'Flowers' => 'Wrapped_Bouquet'
},


valid hash taken from dumper

$VAR1 = 'Pillows';
$VAR2 = {
'Non-Allergenic Pillow' => '0',
'Hollow-Fibre Pillow' => '0',
'Goose Down Pillow' => '0'
};
$VAR3 = 'Flowers';
$VAR4 = {
'Bouquet in a Vase' => '45',
'Arranged Cut Flowers' => '35',
'Wrapped Bouquet' => '55'
};



I need to output :
$options{$key};
$options{$value}
$valid_keys{$third_value} - (the numeric value)

here is my code so far. pointers very very welcome.

bazz


sub show_chosen_options {

my %valid_keys = build_valid_keys();
my %options = % {$session->param('options') };
print Dumper %valid_keys; # outputs ok

while (my($option_category, $option_name) = each(%options)){

while (my ($valid_category, $valid_name, $valid_cost) = each (%valid_keys))
{

print qq( oc =$option_category => on=$option_name => v_c = $valid_cost <br />);
}
}
}

KevinADC
08-09-2008, 06:25 AM
this can't be correct:

while (my ($valid_category, $valid_name, $valid_cost) = each (%valid_keys))

each() returns a 2 item list when used in list context, not a three item list. $valid_cost will be undef.

You should use a reference when printing data structures with Data::Dumper

print Dumper \%valid_keys;

you will see the difference in the output when you do that. To me the output makes a lot more sense when you pass references to Dumper.

Try this:


while (my($option_category, $option_name) = each(%options)){
print qq( oc = $option_category =>
on = $option_name =>
v_c = $valid_keys{$option_category}{$option_name}
<br />);
}

bazz
08-09-2008, 12:21 PM
That's Great Kevin.

Thank you.

It's much bettere than what I rustled up since posting :rolleyes:


while (my($option_category, $option_name) = each(%options)){
$sth->execute($business_id, $option_name);

while (my @fields = $sth->fetchrow_array )
{
$fields[2] = sprintf("%.2f", $fields[2]);
if ($fields[2] eq '0.00' ) { $fields[2] = 'No Charge';}
print qq( <td class='left_side'>$option_name</td><td class='right_side'>$fields[2]</td> );

}



bazz