Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: How Perl can push array into array and then how retrieve

by Marshall (Canon)
on Nov 25, 2021 at 07:53 UTC ( #11139106=note: print w/replies, xml ) Need Help??


in reply to How Perl can push array into array and then how retrieve

You understand what an array is. If you push an array onto another array, you just wind up with a single bigger single dimensional array. A multi dimensional array is an array of references to array. Push a reference to the @e array onto the @f array. push @f,\@e; I suppose you could write, push @f,[@e], but that would expand @e into an anonymous array and then make a reference to that newly created anon array.

BTW, there can be huge problems if you don't use "my" variables. "my @e" creates a brand new @e array every time this is seen. The push, pushes a reference to the "current @e array". When the loop comes around again, a whole new different @e array is created.

use strict; use warnings; use Data::Dump qw(pp); my @f; for my $i (0..40) { my @e=($i+=2, $i+1); #same as ($i+2, $i+3) push(@f,\@e); } pp \@f; __END__ Not sure if this is what you want, but this code results in: [ [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 10], [10, 11], [11, 12], [12, 13], [13, 14], [14, 15], [15, 16], [16, 17], [17, 18], [18, 19], [19, 20], [20, 21], [21, 22], [22, 23], [23, 24], [24, 25], [25, 26], [26, 27], [27, 28], [28, 29], [29, 30], [30, 31], [31, 32], [32, 33], [33, 34], [34, 35], [35, 36], [36, 37], [37, 38], [38, 39], [39, 40], [40, 41], [41, 42], [42, 43], ]
Update: Now that I think about it, $i+=2 is an extremely bad idea. $i is the loop variable. You are asking for big trouble if you attempt to modify the loop variable while you are within the for loop. It does work here, but in general, I would avoid it and go with my ($i+2, $i+3) formulation.

Replies are listed 'Best First'.
Re^2: How Perl can push array into array and then how retrieve
by abdan (Acolyte) on Nov 25, 2021 at 14:07 UTC
    "Now that I think about it, $i+=2 ..."
    I think OP was meaning to have step in Perl for loop.
      There is more than one way to do it.

      You can compute calculate the numbers from a simple sequence:

      my @f; for my $i (0 .. 20) { my @e = (2 * $i, 2 * $i + 1); push @f, \@e; } print map "(@$_)", @f
      or you can use the C-style loop to skip over the unwanted numbers:
      for (my $i = 0; $i <= 40; $i += 2) { my @e = ($i, $i + 1); push @f, \@e; }
      or you can use grep to filter the numbers you want:
      for my $i (grep 0 == $_ % 2, 0 .. 40) { my @e = ($i, $i + 1); push @f, \@e; }
      etc.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      here is another way to have a step in this kind of loop:

      for my $i (map { $_ * 2 } 0..40/2){ ... }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11139106]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2022-05-28 02:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (98 votes). Check out past polls.

    Notices?