Here is my code where i want to  print one hash with one key undefined . I have 
condition print if key exist .

I guess it should not print value for undefined key , but it does .

#!/usr/bin/perl -w

@array = ("abc", 123, "dfg" ,456, "xsd",undef);
%hash = reverse @array;

foreach $k (keys %hash){
     print "key $k value $hash{$k}\n" if(exists $hash{$k});
 }

And another issue is , I want to write one line code for printing hash like 
below:

 print "key $k value $hash{$k}\n"  foreach $k (keys %hash);

But it fails with error :
syntax error at hash_test2.pl line 16, near "$k ("
Execution of hash_test2.pl aborted due to compilation errors.



Please let me where I am going wrong .

-Sunita
                                          

Reply via email to