Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by abdan (Acolyte)
on Nov 25, 2021 at 14:07 UTC ( #11139116=note: print w/replies, xml ) Need Help??


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

"Now that I think about it, $i+=2 ..."
I think OP was meaning to have step in Perl for loop.
  • Comment on Re^2: How Perl can push array into array and then how retrieve

Replies are listed 'Best First'.
Re^3: How Perl can push array into array and then how retrieve
by choroba (Archbishop) on Nov 25, 2021 at 14:16 UTC
    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]
Re^3: How Perl can push array into array and then how retrieve
by bliako (Monsignor) on Nov 25, 2021 at 15:31 UTC

    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://11139116]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2022-05-26 02:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (92 votes). Check out past polls.

    Notices?