Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy associative array question 1

Status
Not open for further replies.

rdyoll

Technical User
Aug 11, 2003
39
US
Hi,
In my form I have a select box with the name="beverage".
I have 3 options: coffee, tea and milk. I have in my script set up an associative array:

$beverage = param('beverage');
%beverage = (coffee => 'Folgers',
tea => 'Lipton',
milk => 'DairyFresh');

foreach $_(keys %beverage) {
$beverage = $beverage{$_};
}
...and then further in the script I want to see the value chosen from the drop-down menu like this...

print "You chose $beverage.";

...but the value never changes...it's always the last item in the array...DairyFresh
Why?
Where am I going wrong?
 
foreach $_(keys %beverage) {
$beverage = $beverage{$_};
}

$beverage is being assigned to each value in the hash, but remains with DairyFresh as its the last value in the hash

HTH
--Paul
 
...ok, Paul, so what would one do to have the proper selected value displayed?
 
...if I do this:

foreach $_(keys %beverage) {
$beverage = $beverage{$_};
print "$_ => $beverage{$_}\n";
}

it will return:

coffee => Folgers
tea => Lipton
milk => DairyFresh

...but outside the loop, if I do this:

print $beverage;

if the chosen value was coffee, it still prints DairyFresh
Anyone??
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top