Unpacking an ArrayRef into X individual arguments

Given an arrayref that contains other arrayrefs, is it possible to zip nested arrayrefs together to use a zip function from List::MoreUtils?

For example, this array:

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)],
];

I would like to pin each line together to get transpose. Expected Result:

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

My initial attempts:

# 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)

I am sure there is a way to do this elegantly, I just do not see it. Any help would be greatly appreciated.

+4
source share
2 answers

The function is zipextremely annoying because it uses a (\@\@;\@\@\@...)prototype or something so crazy. You have to do ampersand call to override the prototype: &zip(@$matrix).

, zip it ( ,

[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];
    }
}
+6

transpose Array::Transpose:

use strict;
use warnings;

use Array::Transpose qw(transpose);

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)],
];

my @array=transpose($matrix);

use Data::Dump;
dd \@array;

:

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

Source: https://habr.com/ru/post/1538424/


All Articles