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

Suppose I make an array of arrays..

#!/usr/bin/perl -w my @arr=('a', 'b', 'c'); my @aroa; push(@aroa, [@arr]);

If I would like to print the first element in the first subarray I can do:

print"$aroa[0][0]\n";

But is there any way that I could print out the entire subarray in just one row without any loops?

print"$aroa[0]\n";

This doesn´t work, neighter does:

print"@$aroa[0]\n";

Thank you for any help with this.

Edit: g0n - code tags

2006-04-10 Retitled (AROA -> AOA) by GrandFather to aid searching
Original title: 'How to print subarray of AROA?'

Replies are listed 'Best First'.
Re: How to print subarray of AOA?
by McDarren (Abbot) on Apr 09, 2006 at 13:16 UTC
    That's not an array of arrays (or list of lists) at all. When you do:
    push(@aroa, @arr);
    all you are doing is pushing the contents of @arr into @aroa. And because @aroa is previously empty, what you have done is just made a copy of @arr.

    And the line:

    print"$aroa[0][0]\n";
    will just give you an error (if you are running under strict) as follows:
    Can't use string ("a") as an ARRAY ref while "strict refs" in use at a +rr.pl line 9.
    So...to get a LoL (list of lists), what you need to do is:
    push(@aroa, [@arr]);
    A couple of tips:
    • Always use strict!!
    • Whenever you are experimenting with more complex data structures, Data::Dumper::Simple can help you to better visualise what's going on

    Cheers,
    Darren :)

      There is something wrong!!!. In the example my line:
      push(@aroa, [@arr]);

      ..has been has written as:
      push(@aroa, @arr);

      The square brackets caused the text to become a link to nowere as you can see. Sorry for the confusion this has coused. I have not thought that I can create an array by push(@aroa, @arr); Maybee you could tell me how to do to write square brackets around an array and beeing able to see them in the final text. "print"@{$aroa[0]}\n";"
      Was the answer I was looking for.
      Are the other possibilities to write the same thing?
        Yes, use <code> tags, even around short pieces of code. It's still code after all.
        and to generalize on moklevat's CORRECT answer, please read Writeup Formatting Tips for more information on formatting posts, with special attention to the section on "Special characters" and the one below, re html tags.
Re: How to print subarray of AOA?
by xdg (Monsignor) on Apr 09, 2006 at 13:15 UTC

    You just need some brackets to clarify the order of operations -- retrieve the array reference and then dereference it explicitly:

    print"@{$aroa[0]}\n";

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: How to print subarray of AOA?
by tirwhan (Abbot) on Apr 09, 2006 at 13:15 UTC

    Your push will not create an array of arrays, but rather just append @arr to @aroa. Try

    push(@aroa,\@arr);

    instead.

    To answer your question, printing out an array is best done with join:

    print join(",",@{$aroa[0]})."\n";

    All dogma is stupid.