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

Why doesn't this work?
#!/usr/bin/perl5 -w ########################################################## %fred = (1 => "un", 2 => "deux", 3 => "trois"); %mary = (1 => "eins", 2 => "zwei", 3 => "drei"); print "\nfred:"; for (keys %fred) {printf "\t%3s: %-8s", $_, $fred{$_}}; print "\nmary:"; for (keys %mary) {printf "\t%3s: %-8s", $_, $mary{$_}}; print "\n"; @nm = qw(fred mary); for $n (@nm) {print "\n$n:"; for (keys %{$n}){printf "\t%3s: %-8s", $_, ${$n}{$_}}} my %fred1 = (1 => "yksi", 2 => "kaksi", 3 => "kolme"); my %mary1 = (1 => "uno", 2 => "due", 3 => "tre"); use strict; my @nm1 = qw(fred1 mary1); print "\n"; for my $n (@nm1) { print "\n$n:"; for my $m (keys %{$n}) { printf "\t%3s: %-8s", $m, ${$n}{$m}; } } print "\n"; __END__ (svmoloch:szhxrv) $ ./test_hashname.pl fred: 1: un 2: deux 3: trois mary: 1: eins 2: zwei 3: drei fred: 1: un 2: deux 3: trois mary: 1: eins 2: zwei 3: drei Can't use string ("fred1") as a HASH ref while "strict refs" in use at + ./test_hashname.pl line 27. fred1:

Replies are listed 'Best First'.
Re: string as a HASH ref
by hardburn (Abbot) on Jun 18, 2003 at 14:22 UTC

    Thou shalt not use symbolic refs unless thou has a really good reason. Here's why.

    One "really good reason" is for playing with the symbol table, which can be great fun and (among other things) makes generating almost-identical accessors and mutators for objects really easy. But for just accessing a variable, don't bother.

    ----
    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: string as a HASH ref
by tall_man (Parson) on Jun 18, 2003 at 14:22 UTC
    You could make it work by putting a "no strict 'refs';" in front of it, but you shouldn't. Don't use variables as variable references. Instead, make a hash of hashes.
    my %names; $names{fred1} = {1 => "un", 2 => "deux", 3 => "trois"}; $names{mary1} = {1 => "eins", 2 => "zwei", 3 => "drei"}; ... print $names{$n}{$m};
Re: string as a HASH ref
by ant9000 (Monk) on Jun 18, 2003 at 14:17 UTC
    Because of your 'use strict' pragma; try this instead:
    for my $n (@nm1) { print "\n$n:"; no strict 'refs'; for my $m (keys %{$n}) { printf "\t%3s: %-8s", $m, ${$n}{$m}; } }

    and then take a look at perldoc perlref (search for strict).
Re: string as a HASH ref
by Bilbo (Pilgrim) on Jun 18, 2003 at 14:25 UTC

    Beacause this: keys %{$n} doesn't do what you think it does. This says 'list the keys in the hash which $n is a reference to', not 'list the keys in the hash of which $n is a string containing the name.'

    You almost certainly don't want to use variables to specify variable names - use a hash instead. Put all (both) your hashes into another hash. Ie:

    my %numbers = {fred1 => \%fred1, mary1 => \%mary1};

    Try looking at perlref or this tutorial for examples of how to create and use complex data structures such as hashes of hashes.

    Also, why not put use strict at the top of the program. If it's worth using (which it is) why not use it on the whole program?

Re: string as a HASH ref
by GreyOwl (Initiate) on Jun 18, 2003 at 15:22 UTC
    I am most impressed and humbly grateful for your prompt help! GreyOwl