perl – 将ArrayRef解包为X个独立的参数

前端之家收集整理的这篇文章主要介绍了perl – 将ArrayRef解包为X个独立的参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
给定包含其他arrayrefs的arrayref,是否可以使用 List::MoreUtils中的zip函数将嵌套的arrayrefs压缩在一起?

比如给出这个arrayref:

my $matrix = [
   [qw( 1  2  3  4)],[qw( 5  6  7  8)],[qw( 9 10 11 12)],[qw(13 14 15 16)],[qw(17 18 19 20)],];

我想将每一行压缩在一起,这样我就可以进行转置.预期产量:

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

我最初的尝试是:

# I had hoped the function would unpack the arguments
zip @$matrix; 
# ERROR: Not enough arguments for List::MoreUtils::mesh at spiral.pl line 17

# I thought this slice would suffice to unpack them
zip @$matrix[1..scalar @$matrix-1]; 
# ERROR: Type of arg 1 to List::MoreUtils::mesh must be array (not array slice)

我确信有一种方法可以优雅地做到这一点,我只是没有看到它.任何帮助,将不胜感激.

解决方法

拉链功能非常烦人,因为它使用 a (\@\@;\@\@\@...) prototype或类似疯狂的东西.你必须做一个&符号来覆盖原型:& zip(@ $matrix).

但是,你试图转置矩阵,而不是拉链(这将生成一个连续的列表,如

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

我们可以将natatime迭代器与zip结合使用:

my $iter = natatime @$matrix,&zip(@$matrix);
my @transposed;
while (my @column = $iter->()) {
    push @transposed,\@column;
}

哪个有效,但这严重地过分了问题.我们只是交换指数:

my $transposed = [];
for my $i (0 .. $#$matrix) {
    for my $j (0 .. $#{ $matrix->[0] }) {
        $transposed->[$j][$i] = $matrix->[$i][$j];
    }
}

猜你在找的Perl相关文章