in reply to How Perl can push array into array and then how retrieve
I'm guessing somewhat regarding what you want to achieve.
Always put strict and warnings at the top of your code. In this instance, that would have alerted you to the fact that there was something wrong with $f.
I was unsure why you wanted to increment $i inside the loop. In my version, I changed $i+=2 to $i+2.
Here's a couple of ways of doing what I think you want:
#!/usr/bin/env perl use strict; use warnings; my @f; # Modification of your code for my $i (0..2) { my @e = ($i+2, $i+1); push @f, [@e]; } # A more succinct way to achieve it for my $i (3..5) { push @f, [$i+2, $i+1]; } # For demo purposes use Data::Dump; dd \@f;
Output:
[[2, 1], [3, 2], [4, 3], [5, 4], [6, 5], [7, 6]]
See also: "perlintro: Arrays"; "perldsc - Perl Data Structures Cookbook"; "perllol - Manipulating Arrays of Arrays in Perl".
— Ken
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How Perl can push array into array and then how retrieve
by kcott (Archbishop) on Nov 25, 2021 at 05:49 UTC | |
by jwkrahn (Abbot) on Nov 25, 2021 at 07:15 UTC | |
by cavac (Parson) on Nov 30, 2021 at 15:05 UTC | |
by kcott (Archbishop) on Dec 02, 2021 at 08:14 UTC | |
by cavac (Parson) on Dec 03, 2021 at 12:22 UTC |
In Section
Seekers of Perl Wisdom