in reply to Array of array

How can I print only the contents of @arr2

Instead of pushing the array as list context, I you push the reference of each array you can take the @arr2 separately easily

use strict; use warnings; use Data::Dumper; my @mainarr; my @arr1=qw(a b c d e); my @arr2=qw(f g h i j); my @arr3=qw(k l m n o); push(@mainarr,\@arr1); push(@mainarr,\@arr2); push(@mainarr,\@arr3); # print Dumper \@mainarr; my $index=0; foreach (@mainarr){ print @{$mainarr[$index]} if $index == 1; $index+=1; }
Vinoth,G

Replies are listed 'Best First'.
Re^2: Array of array
by ig (Vicar) on Jun 04, 2009 at 08:40 UTC

    or, instead of a loop over all of @mainarr, you might use:

    print @{$mainarr[1]};
      using foreach?

      Yes, We can directly access the array element as you said, but Anonymous Monk asked in foreach thats why I used foreach here.

      Vinoth,G
        Anonymous Monk asked in foreach thats why I used foreach here.

        So he did, and I ignored this aspect of his request. Considering it now, I suggest the following:

        foreach my $element (@{$mainarr[1]}) { print "$element\n"; }