Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use a variable as a hash name to extract certain values. I have looked through some pertinent doc including perlref and I can’t seem to figure out what I am doing wrong. Here is a bit of sample code that reproduces the problem that I am having.
use strict; no strict "refs"; use CGI qw/:standard/; my (@compare, %other0, @catnames); %other0 = ( 'cat0'=>'3', 'cat1'=>'3' ); @compare = ('testone', 'testtwo'); @catnames = ('other0','other1'); print "this one works: $other0{'cat1'}\n"; print "problem here: ${$catnames[0]}{'cat1'}\n $catnames[0]->{'cat1'}" +;

Replies are listed 'Best First'.
Re: Referencing a hash
by broquaint (Abbot) on Jul 07, 2003 at 16:26 UTC
    You appear to be trying reference the hash symbolically, where as you need to store a reference to the %other0 hash to get your code working e.g
    use strict; my (%other0, @catnames); %other0 = ( 'cat0' => 'a string', ); ## create reference to %other0 hash @catnames = (\%other0); print "access hash: ", $catnames[0]->{'cat0'}; __output__ access hash: a string
    See. perlreftut and tye's References quick reference for a couple of good starters on references.
    HTH

    _________
    broquaint

Re: Referencing a hash
by gellyfish (Monsignor) on Jul 07, 2003 at 16:34 UTC

    I'm not going to tell you how to fix this because you almost certainly don't want to be doing that - essentially you are looking to introduce a second level of hash-like functionality into your logic which you can do without the use of the horrible soft references - observe:

    use strict; my %hashes = ( other0 => { 'cat0'=>'3', 'cat1'=>'3' }, other1 => {} ); my @catnames = ('other0','other1'); print $hashes{$catnames[0]}->{cat0},"\n";
    I would recommend taking a look at perlref and perlreftut in the first instance if you have any more questions about this.

    /J\
    
Re: Referencing a hash
by hardburn (Abbot) on Jul 07, 2003 at 16:24 UTC

    Don't use symbolic refs to access variables. There are few good uses of symbolic refs, and you don't appear to be using any of them.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: Referencing a hash
by bobn (Chaplain) on Jul 07, 2003 at 19:11 UTC
    If you really want to interpolate a variable into the name of another variable, as you are doing, which is called a symbolic reference, you need to turn off strict 'refs'as in:
    # UNTESTED. print "this one works: $other0{'cat1'}\n"; { no strict 'refs' print "problem here: ${$catnames[0]}{'cat1'}\n $catnames[0]->{'cat +1'}"; }

    This turns off strictness as regards symbolic references between the innermost contiaing braces.

    However:

    • this only fixes the first half of the 'print "problem here...", the second interpolation is still wrong.
    • As others have noted, you really should not use symboic references if you can use the other kind
    --Bob Niederman, http://bob-n.com
Re: Referencing a hash
by Anonymous Monk on Jul 07, 2003 at 20:13 UTC
    Thank you all for your help, you have helped me see the error in my ways.