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

Fellow Monks,

Thanks for all your help yesterday, I learned a lot. I beg one more favour. Take this snippet:
my @aaa = qw (11 22 33); my @bbb = qw (44 55 66); my @ccc = qw (77 88 99); my @masterarray = \(@aaa, @bbb, @ccc); # This doesn't work, appears to print a REF print \$_ . "\n" for @masterarray; # This also doesn't work, appears to print an ARRAY print $_ . "\n" for @masterarray;
Could somebody please explain to me the difference in the output between the two print lines above?
I can print the contents of each referenced array using something like:print "@$_\n" for @masterarray;
But what I'd like to do is print the name of each called array to screen from @masterarray, for example:
@aaa @bbb @ccc
Thanks in advance,
Jonathan

Replies are listed 'Best First'.
Re: printing array item reference names
by ambrus (Abbot) on Jan 14, 2005 at 12:17 UTC

    Let's store both the names for the arrays, and a ref to them:

    use warnings; use strict; my @aaa = qw (11 22 33); my @bbb = qw (44 55 66); my @ccc = qw (77 88 99); my @masterarray = ([aaa => \@aaa], [bbb => \@bbb], [ccc => \@ccc]); print $$_[0], ": ", @{$$_[1]}, "\n" for @masterarray;
Re: printing array item reference names
by Joost (Canon) on Jan 14, 2005 at 12:01 UTC
    A variable name has nothing to do with the values in it. You basically can't get the name of a variable from a value without some really advanced/unreliable trickery, and I've personally never had any use for this functionality.

    If you want to print the names, you need to store the names in @masterarray.

    my @masterarray = qw(@aaa @bbb @ccc); print $_ . "\n" for @masterarray;

Re: printing array item reference names
by Random_Walk (Prior) on Jan 14, 2005 at 12:02 UTC

    You are storing references in the masterarray, these are pointers to the actual data in the aaa, bbb and ccc arrays, not pointers to the array names. To get back to the array names you can do some voodoo searching of the symbol table with the reference values (returned in your second printing example).

    The other way to do it would be to turn the problem on its head and store the actual names in masterarray and then use eval to find the values in there when you need them, something like this.

    #!/usr/bin/perl use warnings; use strict; my @aaa = qw (11 22 33); my @bbb = qw (44 55 66); my @ccc = qw (77 88 99); my @masterarray = qw(aaa bbb ccc); print "$_ contains\t". (join ", ", eval "\@$_") . "\n" for @masterarra +y;

    update

    These arrays being lexical will not show in the symbol table, sorry. They are on the scratchpad space used for lexicals and there is a module to look into it called PadWalker. Good Luck !

    you can also simplify the print doing it this way around if you use a symbolic ref (need to turn of strict refs for the scope)

    { no strict "refs"; print "$_ contains\t". (join ", ", @$_) . "\n" for @masterarray; }

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: printing array item reference names
by ambrus (Abbot) on Jan 14, 2005 at 12:03 UTC