Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring Arrays in Perl6 with Multiple Ranges

Tags:

raku

I'm attempting to create an array in Perl6 with two different ranges as I would have in Perl5:

my @cols = (3..9, 11..16);
use Data::Printer:from<Perl5>;
p @cols; exit;

However, this creates a 2-D array

[
    [0] [
            [0] 3,
            [1] 4,
            [2] 5,
            [3] 6,
            [4] 7,
            [5] 8,
            [6] 9
        ],
    [1] [
            [0] 11,
            [1] 12,
            [2] 13,
            [3] 14,
            [4] 15,
            [5] 16
        ]
]

when it should be a 1d array, as (3..9, 11..16) would have been in Perl5:

[
    [0]  3,
    [1]  4,
    [2]  5,
    [3]  6,
    [4]  7,
    [5]  8,
    [6]  9,
    [7]  11,
    [8]  12,
    [9]  13,
    [10] 14,
    [11] 15,
    [12] 16,
    [13] 17,
    [14] 18,
    [15] 19,
    [16] 20
]

I can easily get around this, of course, with append but how can I get the Perl5 result of (3..9, 11..16) in Perl6 in one line only?

like image 510
con Avatar asked Jun 28 '19 17:06

con


1 Answers

The .. operator creates a Range object and thus you're creating a list with 2 Range's in it. In order to make a list of the values in the Ranges you need to unroll them and flatten them.

Here are two ways:

  • Use a Slip:

     my @cols = (|(3..9), |(11..16))
    
  • or flat:

     my @cols = (3..9, 11..16).flat
    
like image 172
Håkon Hægland Avatar answered Oct 04 '22 14:10

Håkon Hægland