简体   繁体   中英

Is there subscript syntax to extract a diagonal from a 2D Array?

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?

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 0 th, 1 st, and 2 nd rows.

$ is an anonymous Scalar state variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM