1

How do we correctly push array into array and then retrieve each of the outer array (explain the both asked each separated clearly of other)?

illustrated

my @f; my @e;
for $i (0..40) {
    @e=($i+=2, $i+1);
    push(@f,@e); # just it right ? 
}
    
# how go on get it under multi array control
1
  • Could you post an example of what the final array would look like in pseudo code. Commented Nov 25, 2021 at 3:24

2 Answers 2

6

Data::Dumper is a good tool to explore what is going on here. It makes it easy to visualise your data structures.

If we run your code and then display @f using Data::Dumper, we get this:

$VAR1 = [
          2,
          3,
          3,
          4,
          4,
          5,
          5,
          6,
          6,
[ ... snip ... ]
          40,
          40,
          41,
          41,
          42,
          42,
          43
        ];

So that's not doing what you want. If you push an array onto another array then Perl just adds each element from the second array to the end of the first array. It becomes impossible to tell which element belongs to which array. This is known as "array flattening".

It happens because an element in an array can only hold a single scalar value. And another array is not a scalar value. But we can take a reference to an array and that is then a scalar value. You get a reference to an array using \.

The change to your code is simple:

my @e=($i+=2, $i+1); # declare @e inside the loop
push(@f,\@e); # Take a reference

And the output we now get is:

$VAR1 = [
          [
            2,
            3
          ],
          [
            3,
            4
          ],
          [
            4,
            5
          ],
[ ... snip ... ]
          [
            39,
            40
          ],
          [
            40,
            41
          ],
          [
            41,
            42
          ],
          [
            42,
            43
          ]
        ];

You can clearly see the individual "sub-arrays" inside your main array.

There's more about this in the perllol manual page and you can find out far more about references in perlreftut and perlref.

Sign up to request clarification or add additional context in comments.

2 Comments

pretty cool explanation. If I ever had a need to store arrays then I used hash instead of array into an array.
@SamB: But that gives you exactly the same problem. Elements in an array and values in a hash can both only be scalar values - so you need to create references in order to store either arrays or hashes there.
2

Do you mean something like this?

#! /usr/bin/env perl

use warnings;
use strict;
use utf8;
use feature qw<say>;
use Data::Dumper;

my @f = ();

for my $i (0..4) {
    my @e = ($i+=2, $i+1);
    push(@f,\@e);
}

say Dumper(\@f);

exit(0);

1 Comment

Explanation: Array elements are scalars, so an array can't be placed in an array. But a reference to one can be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.