in reply to How can increase number sequence in a variable

Hi, welcome.

Please see

Also see:

When you pass arrays to a loop each element will be handled separately.

perl -Mstrict -wE 'my @x=1..2; my @y=3..4; say "$_" for (@x, @y);' 1 2 3 4
If you want the arrays to be handled as units, you must pass a reference to them instead:
perl -Mstrict -wE 'my @x=1..4; my @y=1..4; say "$_" for (\@x, \@y);' ARRAY(0xcd9588) ARRAY(0xcd9510)
If you then want to access the elements of the individual arrays you will have to dereference them in the loop:
perl -Mstrict -wE 'my @x=1..2; my @y=3..4; say "@{$_}" for (\@x, \@y); +' 1 2 3 4

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: How can increase number sequence in a variable
by karlgoethebier (Abbot) on Nov 17, 2017 at 18:09 UTC
    "...pass a reference"

    It depends...

    perl -Mstrict -E 'my @x=(1..2); my @y=(3..4); say join ",", map{$_} (@x,@y)'

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      Hi kgb, I'm not certain what you are intending to show there.

      As I understood it the OP wanted to keep the arrays separated in the loop. And if they are to be contatenated with join, you don't need map.

      perl -Mstrict -E 'my @x=(1..2); my @y=(3..4); say join "->", (@x,@y)' 1->2->3->4


      The way forward always starts with a minimal test.
        "...what you are intending to show..."

        Good question. Perhaps the Roussillon i had last night wasn't so inspiring as i hoped.

        Presumably i meant something like this:

        #!/usr/bin/env perl use strict; use warnings; use Data::Dump; my @x = ( 1 .. 2 ); my @y = ( 3 .. 4 ); dd \@x; dd \@y; my @result = map { $_ } ( @x, @y ); dd \@result; dd \@x; dd \@y; __END__ karls-mac-mini:monks karl$ ./1nickt.pl [1, 2] [3, 4] [1 .. 4] [1, 2] [3, 4]

        The source arrays are intact and no need to deref here.

        If this is helpful to the OP or good is another question ;-)

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help