How can I use a variable variable name? 
Author Message
 How can I use a variable variable name?


>I think I've managed to get this to work before but I can't seem to get
>it working now. I want to access various hashes- something like:

> my (%A,%B,%C);
> my $x='A';
> $A{cat}=qq(dog);
> my $y=${$x}{cat}; # or just $$x{cat}
> print "val is $y\n";

>which doesn't work since I cannot use $x as a hashref (its not

Instead of trying to access hashes by name, I suggest you use a hash
of hashes.  The code would look like this:

        my %hash;
        my $x = 'A';
        $hash{A}{cat} = qq(dog);
        my $y = $hashes{$x}{$cat};
        print "val is $y\n";

>I read perlref again today but I didn't see this in there, but I may
>have missed it as its very long.

You may want to have a look at 'perlreftut', if you have it.  It is a
much shorter tutorial of just the few most important uses of references.

If you don't have it, you may find a copy at


($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print

Mon, 29 Sep 2003 08:26:52 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Tough (or stupid =) Q: Using a variable with a variable name

2. using contents of a scalar variable as a variable name

3. Using a variable as a variable name

4. using a variable as a variable name?

5. Using Variables as Variable names

6. FAQ: Using a variable as a variable name

7. Runtime 200 in delay, - How do I test for this fix on a slow cpu?


9. Delphi 1 and TQuery

10. Interested in a wavecompressor?

11. Name space switching using variable package names

12. create variable name from a variable in perl


Powered by phpBB® Forum Software