交换矩阵中的第一列和最后一列

时间:2018-06-23 21:17:41

标签: powershell matrix perl6

例如,我们有一个矩阵。

 1,  2,  3,  4,  5
 6,  7,  8,  9, 10
11, 12, 13, 14, 15
16, 17, 18, 19, 20
21, 22, 23, 24, 25

也许解决Perl6主题标题中听起来最简单的问题的方法是

my @matrix = [1..5], [6..10], [11..15], [16..20], [21..25];
@matrix.map:{.[0,*-1] = .[*-1,0]};

结果

 5,  2,  3,  4,  1
10,  7,  8,  9,  6
15, 12, 13, 14, 11
20, 17, 18, 19, 16
25, 22, 23, 24, 21

在PowerShell中如何做同样漂亮?

1 个答案:

答案 0 :(得分:4)

您翻译为PowerShell的代码段如下所示:

$matrix = (1..5), (6..10), (11..15), (16..20), (21..25)
$matrix | ForEach-Object { $_[0], $_[-1] = $_[-1], $_[0] }