12

I mostly can follow the syntax to 'drill down/slice' into an array with multiple dimensions (and flattening) on the docs page. A very cool feature. For example given:

my @a=[[1,2,3],
       [4,5,6],
       [7,8,9]];

I can select column 2 of the above using:

say @a[0,1,2;1]; #This output (2,5,8)

Is it possible to extract the diagonal (1,5,9) in a similar compact syntax?

jjmerelo
  • 22,578
  • 8
  • 40
  • 86
drclaw
  • 2,463
  • 9
  • 23

1 Answers1

12
say @a[ 0,1,2 ; { $++ } ] ; # (1 5 9)

So instead of 1, which evaluates to 1, I've used { $++ }, which is a Block.

When Raku encounters a callable code object as a subscript value, it calls it once for each slice it's evaluating, in this case the 0th, 1st, and 2nd rows.

$ is an anonymous Scalar state variable.

raiph
  • 31,607
  • 3
  • 62
  • 111
  • 1
    ```say @a[0,1,2; { $++ }];``` awesome answer from @raiph - shows why raku rocks ... just to mention that the '.' is optional. – librasteve Dec 13 '20 at 21:38
  • @p6steve. I too think that it's nice and think that that's because Raku rocks. I've no idea why I decided to write the `.`. Thanks for commenting; I've now removed it because it's very ***un***idiomatic. – raiph Dec 13 '20 at 22:59