Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Array of array

by Anonymous Monk
on Jun 04, 2009 at 07:36 UTC ( #768324=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am having an array like this
@mainarr; @arr1=qw(a b c d e); @arr2=qw(f g h i j); @arr3=qw(k l m n o); push(@mainarr,@arr1); push(@mainarr,@arr2); push(@mainarr,@arr3);
So mainarr will be having three arrays. How can I print only the contents of @arr2 from mainarr using foreach?

Comment on Array of array
Download Code
Re: Array of array
by Corion (Pope) on Jun 04, 2009 at 07:40 UTC
    So mainarr will be having three arrays.

    No:

    use strict; 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 \@arr1; print Dumper \@arr2; print Dumper \@arr3; print "-- mainarr\n"; print Dumper \@mainarr;

    If you want to keep the structure of @arr1 etc., you will have to store references to the arrays in @mainarr:

    ... push(@mainarr,\@arr1); push(@mainarr,\@arr2); push(@mainarr,\@arr3); print Dumper \@mainarr;

    Then you can get at the values in @arr2 via $mainarr->[1]. Also see References Quick Reference.

Re: Array of array
by nagalenoj (Friar) on Jun 04, 2009 at 07:44 UTC
    Dear monk,

    We cant separate as what you have asked. Since, it has been given in the perldoc for push that, this function is just appending a list to an array.

    But, it is possible to push a reference to the array and get back a particular list of elements. like below.,

    push @mainarr,\@arr1; print Dumper @mainarr->[1];

    UPDATE: Similar answer has been given above when I am typing this, sorry for the repetition
      ...sorry for the repetition.
      There's no need to be sorry. I like to see a few versions of a solution. I often pick up handy techniques comparing different approaches.

      A minor nit though, your snippet won't compile throws a warning under strict and warnings. This is a bit better.

      #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @arr1 = qw{one two three}; my @arr2 = qw{four five six}; my @mainarr; push @mainarr, \@arr1, \@arr2; #print Dumper @mainarr->[1]; # <- line 12 #Using an array as a reference is deprecated at C:\perm\monk.pl line 1 +2. #C:\perm\monk.pl syntax OK print Dumper $mainarr[1]; __DATA__ $VAR1 = [ 'four', 'five', 'six' ];

      My motto: "Test it _before_ you post it." :-)

      update: it does compile ("syntax OK") but it throws a warning

Re: Array of array
by vinoth.ree (Parson) on Jun 04, 2009 at 07:52 UTC
    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

      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
Re: Array of array
by Utilitarian (Vicar) on Jun 04, 2009 at 08:06 UTC
    Did you test what is in your array?

    Push just adds the values in the second array to the end of the first array. What you have is a flattened list rather than an array of arrays.

    If you want to add a reference to an array you need to push(@mainarr,\@arr1);
    Then you can access the second element of each using

    for $array_ref (@mainarr){ print "$array_ref->[1]\n"; }

    For further info have a look at perldoc perllol

Re: Array of array
by Marshall (Prior) on Jun 04, 2009 at 09:30 UTC
    Below is some code. You don't even have to use foreach if you know that you want the 6th..10th things as shown below.

    There are some folks who like to call @thingie an array, and folks who like to call @thingie a list. I figure there are some valid arguments on both sides, but that is not the point below..

    #!/usr/bin/perl -w use strict; 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); # you have flattened each list into a single list # The "push" is similar to a queue operation # that adds things to the end of the queue. # there are 4 Perl ops that you should read more # about (push,pop,shift,unshift), all of these operate # on lists. print "@mainarr\n"; # that prints: a b c d e f g h i j k l m n o print join(" ",@mainarr[5..9]),"\n"; # this is a list slice and prints the 6th-10th things... # this prints: f g h i j #If you want to maintain the integrity of these things #that you "push" onto @mainarr, then they have to be #grouped together. The way to do that is with a #reference to a list. This creates a LoL, a List of List. push(@mainarr,[@arr1]); push(@mainarr,[@arr2]); push(@mainarr,[@arr3]); print "@mainarr\n"; #will produce something like: #ARRAY(0x182a64c) ARRAY(0x183642c) ARRAY(0x183649c) foreach $list_ref (@mainarr) { print "@$list_ref\n"; } #this prints: #a b c d e #f g h i j #k l m n o
Re: Array of array
by bichonfrise74 (Vicar) on Jun 04, 2009 at 16:19 UTC
    If you create an AoA, then you can print it by doing something like this...
    #!/usr/bin/perl use strict; 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], [@arr2], [@arr3]); print @{$mainarr[1]};

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://768324]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2014-07-31 02:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (244 votes), past polls