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

Monestarians

The following code:

#!/Perl/bin/perl use strict; use warnings; my %test = ('client' => {['11211','11111']=>['Z','Y','X'], ['10900','12345']=>['A','B','C']}); foreach my $key_a (keys(%test)) { foreach my $key_b (keys(%{$test{$key_a}})) { foreach my $i(@{$test{$key_a}{$key_b}}) { print $i."\n"; } } }
will print the values ZYXABC. However I want to know if it is possible to reference the values in the anonymous array that is the key to the value anonymous array. I tried.
#!/Perl/bin/perl use strict; use warnings; my %test = ('client' => {['11211','11111']=>['Z','Y','X'], ['10900','12345']=>['A','B','C']}); foreach my $key_a (keys(%test)) { foreach my $key_b (keys(%{$test{$key_a}})) { print $_ foreach @{$key_b} } }
which yielded a symbolic reference error. Can anyone shed some light on my problem?

I'm sure this is a no-no for good reason (making an anon array a hash key). Basically what I'm tryin to do is for a given client and a given data set, determine actions that should be taken on that dataset. So as to say for data pieces 11111 and 11211 I should do XYZ. So in lieu of listing them out as individual keys I thought that perhaps I could make them an array ref and only list the actions once. Someone guide me please because I'm kinda off I think.

edit Corion reminded me that only strings can be hash keys which explains the symbolic ref value.. since its simply storing the value "ARRAY0x000" rather than the actual ref. So in short.. problem solved.


Grygonos

Replies are listed 'Best First'.
Re: HoHoA with anonymous array refs as the key to access the A
by davido (Cardinal) on Dec 01, 2004 at 17:49 UTC

    Hash keys are stringified, and thus, references can't be hash keys. If you place a reference in a hash key it gets stringified to something like "ARRAY 0x3E0CFF", and can't be resurrected.

    Are you looking for a hash slice?

    my %hash; @hash{ 'A', 'B', 'C' } = ( 1, 2, 3 );

    Incidentally (and a bit off your original topic), the fact that references stringify when used as hash keys -- including blessed refs -- is the basis for keeping track of inside-out objects:

    package InOut; use strict; use warnings; my %instances; sub new { my( $class, @params ) = @_; my $obj = bless {}, $class; $instances{$obj} = { params => [ @params ]}; return $obj; } sub getparam { my ( $self, $index ) = @_; return $instances{$self}{params}[$index]; } 1; package main; use strict; use warnings; my $thingy = InOut->new(qw/ Bart Homer Marge Maggie Lisa / ); print $thingy->getparam( 2 ), "\n";

    Dave

Re: HoHoA with anonymous array refs as the key to access the A
by Fletch (Bishop) on Dec 01, 2004 at 18:11 UTC

    You might be able to do something like this with Tie::RefHash (which has been core for a while now, IMSMR). Keep in mind that uses tie to do its magic so there'll be extra overhead.

Re: HoHoA with anonymous array refs as the key to access the A
by NetWallah (Canon) on Dec 01, 2004 at 21:04 UTC
    If I understand your question right .. I think THIS is what you are looking for ...
    use Data::Dumper; my $t; my %a=('c'=> {rr=>$t=[4,5,6], # Notice use of $t for temp store ss=>$t}); # which gets assigned HERE .... print Dumper \%a; print qq( $_ = @{$a{c}->{$_}} \n) for qw(rr ss) ---- OUTPUT --- $VAR1 = { 'c' => { 'ss' => [ 4, 5, 6 ], 'rr' => $VAR1->{'c'}{'ss'} } }; rr = 4 5 6 ss = 4 5 6

        ...each is assigned his own private delusion but he cannot see the baggage on his own back.