in reply to using other variables as keys to hash
Both davorg and MZSanford have pointed out above that you're using symbolic references and hinted why that's a bad idea ;-) Just to complete the picture, here's what your code would look like using true or "hard" references.
For more information on the backslash operator for taking references and the syntax for getting at its contents, take a look at the doco on references.use strict 'refs'; # Catches use of symbolic references, # though plain "use strict" would be even better # Quotes are not necessary on the LHS of => %simpsons = ( father => "homer", mother => "marge", son => "bart", daughter => "lisa", ); # Here's the code that uses symbolic refs: #$string = "simpsons"; #@array = ("simpsons"); #%hash = ("family" => "simpsons"); #print("$simpsons{father}\n"); #case1 prints "homer" #print("$$string{mother}\n"); #case2 prints "marge" #print("$$array[0]{son}\n"); #case3 err #print("$$hash{family}{daughter}\n"); #case4 # Here are the hard refs. $string = \%simpsons; @array = (\%simpsons); %hash = (family => \%simpsons); # Not necessary to quote variables to print print $simpsons{father}, "\n"; #case1 prints "homer" print $$string{mother}, "\n"; #case2 prints "marge" print $string->{mother}, "\n"; #also prints "marge" print ${$array[0]}{son}, "\n"; #case3 prints "bart" print $array[0]->{son}, "\n"; #also prints "bart" print ${$hash{family}}{daughter}, "\n"; #"lisa" print $hash{family}->{daughter}, "\n"; #also "lisa" print $hash{family}{daughter}, "\n"; #also "lisa"
HTH
|
|---|