Question : easy question (if else / foreach)

trying to test if key exists in hash
if exists in hash, want to print value and offer to change value (I can change  value ok so didn't include code)
if dosen't exist, want to offer to add (know how, but if else / foreach doesn't work).
now. this should print two trues for you unless you enter a number.  you are supposed to enter apple or sky to get a true, but any sting gets a true.  The if then always tests true.  What is going on here?

#!/usr/bin/perl

%tab=("apple","red","sky","blue");
$val=;
foreach $key(keys(%tab)){
if($val==$key){
print "\ntrue" . "value is " . $tab{$val};
#want to change part would be here}

else{
print "false - no such key";
#want to add section would be here
}}


********************
I want to use the foreach method to look at each key to do this.
I also want to use the if inside the for each.
THanks
...I've added points because I know this is an unusual way to test for the existance of a key.

Answer : easy question (if else / foreach)

= is assignment, == is numeric equality, eq is string equality
Random Solutions  
 
programming4us programming4us