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

I think this should be simple, but did not find an answer in my search of the internet so far. I want to ripple thru an AoA, and print the name of the current array, and its size. This prints the size, but I have not figured out how to include the name of the current array.
foreach (@AoA) { print @{$_} . "___________________________________________________ +__________ __\n";
Thanx

Replies are listed 'Best First'.
Re: Print name of array
by afoken (Chancellor) on Dec 06, 2011 at 13:06 UTC
    print the name of the current array

    What makes you think it has exactly one name? And why would you need that information?

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Print name of array
by choroba (Cardinal) on Dec 06, 2011 at 13:05 UTC
    What do you mean by the name of the array? Its index in the @AoA? To get it, you can just
    for my $index (0 .. $#AoA) { print "$index\t@{ $AoA[$index] }\n"; }
      This prints the index number, and then the contents of the array.I was hoping to get the actual name.
      my @AoA = ( \@AdhLnk, \@MtrLnk, \@SltLnk, \@OthLnk, \@AdhDlk, \@MtrDlk +, \@SltDlk , \@OthDlk ); print "Output loop\n"; for my $index (0 .. $#AoA) { print "$index\t@{ $AoA[$index] }\n"; } foreach (@AoA) { print @{$_} . "_____________________________________________________ +________ __\n"; }

        Then it should have been a HoA:

        my %HoA = ( AdhLnk => \@AdhLnk, MtrLnk => \@MtrLnk, SltLnk => \@SltLnk +, OthLnk => \@OthLnk, AdhDlk => \@AdhDlk, MtrDlk => \@MtrDlk, SltDlk => \@SltDlk, OthDlk + => \@OthDlk ); print "Output loop\n"; for my $name (keys %HoA) { print "$name\t@{ $HoA{$name} }\n"; }

        And most probably there should not have been all those arrays in the first place.

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

        And what would you return for this?
        @AoA = ([qw/Mercury Venus Earth Mars/], [qw/1 1 2 3 5 8 13 21/]);
        From where should Perl know it's planets and fibonacci?

        Update: Also consider

        @x = (1, 2, 3); *y = *x; my @AoA = (\@x, \@y);
        Is it @x or @y?
        A reference by itself does not know what variables points to the same data. This is because the array your reference points to does not have to be accessible through a normal array variable. There could also be multiple variables pointing to the same Data. The reference only knows, what type of data it points to and where that data is. If you need labels for those lists, you might want to use a Hash of Arrays.
      Maybe (well, maybe) OP is looking for
             \@AOA or \%foo

      This might be useful with a meaningful array-name; obviously, it won't help with poorly named (or, named-just-for-illusation) cases, not for anonymous arrays.

Re: Print name of array
by TJPride (Pilgrim) on Dec 06, 2011 at 16:43 UTC
    What you're probably looking for is one of these structures:

    use strict; use warnings; my (@a1, @a2, %HoA, @AoA); @a1 = (1,2,3); @a2 = (2,3,4); %HoA = ( 'a1' => \@a1, 'a2' => \@a2 ); print "$_ : @{$HoA{$_}}\n" for keys %HoA; @AoA = ( ['a1', \@a1], ['a2', \@a2] ); print "$_->[0] : @{$_->[1]}\n" for @AoA; @AoA = ( [@a1, 'a1'], [@a2, 'a2'] ); print pop(@$_) . " : @$_\n" for @AoA;