Perl - 内置函数将两个数组“拉链”在一起?

时间:2022-12-29 21:17:29

I want to merge two arrays of equal length into a single array by taking the first element from array A, the first element from array B; second element from A, second element from B, etc. The following program illustrates the algorithm:

我想通过从数组A获取第一个元素,从数组B获取第一个元素,将两个相等长度的数组合并为一个数组;来自A的第二个元素,来自B的第二个元素等。以下程序说明了算法:

# file zipper.pl
use strict;
use warnings;
use 5.010;

my @keys   = qw/abel baker charlie dog easy fox/;
my @values = qw/a b c d e f/;

# ==> Is there a builtin function that is equivalent of zipper()? <==
#
my %hash = zipper( \@keys, \@values );

while ( my ( $k, $v ) = each %hash ) {
    say "$k=$v";
}

# zipper(): Take two equal-length arrays and merge them (one from A, one from B,
# another from A, another from B, etc.) into a single array.
#
sub zipper {
    my $k_ref = shift;
    my $v_ref = shift;
    die "Arrays must be equal length" if @$k_ref != @$v_ref;
    my $i = 0;
    return map { $k_ref->[ $i++ ], $_ } @$v_ref;
}

Output

产量

$ ./zipper.pl 
easy=e
dog=d
fox=f
charlie=c
baker=b
abel=a

I'm wondering if I've overlooked a builtin function in Perl that will do the equivalent of zipper(). It will be at the innermost loop of the program, and needs to run as fast as possible. If there's not a built-in or a CPAN module, can anyone improve upon my implementation?

我想知道我是否忽略了Perl中的内置函数,它将完全相当于zipper()。它将位于程序的最内层循环中,并且需要尽可能快地运行。如果没有内置或CPAN模块,任何人都可以改进我的实现吗?

4 个解决方案

#1


24  

Others have given good answers for mesh/zip side of the question, but if you are just creating a hash from an array of keys and one of values you can do it with the under-appreciated hash slice.

其他人已经为问题的网格/ zip方面提供了很好的答案,但是如果你只是从一组键和一个值创建一个哈希,你可以用不充分的哈希切片来做。

#!/usr/bin/env perl

use strict;
use warnings;

my @keys   = qw/abel baker charlie dog easy fox/;
my @values = qw/a b c d e f/;

my %hash;
@hash{@keys} = @values;

use Data::Dumper;
print Dumper \%hash;

Addendum

附录

I got to thinking why one may choose one method over the other. I personally think that the slice implementation is as readable as the zip, but others may disagree. If you are doing this often, you may care about speed, in which case the slice form is faster.

我开始思考为什么人们可以选择一种方法而不是另一种方法。我个人认为切片实现与zip一样可读,但其他人可能不同意。如果你经常这样做,你可能会关心速度,在这种情况下切片形式更快。

#!/usr/bin/env perl

use strict;
use warnings;

use List::MoreUtils qw/zip/;
use Benchmark qw/cmpthese/;

my @keys   = qw/abel baker charlie dog easy fox/;
my @values = qw/a b c d e f/;

cmpthese( 100000, {
  zip => sub {
    my %hash = zip @keys, @values;
  },
  slice => sub {
    my %hash;
    @hash{@keys} = @values;
  },
});

results:

结果:

         Rate   zip slice
zip   51282/s    --  -34%
slice 78125/s   52%    --

#2


11  

Since you offered a CPAN idea, there is List::MoreUtils and zip.

由于您提供了CPAN的想法,因此有List :: MoreUtils和zip。

use List::MoreUtils qw(zip);

my @keys   = qw/abel baker charlie dog easy fox/;
my @values = qw/a b c d e f/;

my @zipped = zip @keys, @values;

The contents of @zipped would be:

@zipped的内容是:

abel, a, baker, b, charlie, c, dog, d, easy, e, fox, f

The nice part about using this method is you can zip more than two lists if you wish. Since Perl has no concept of a tuple type, it is almost like a flattening operation.

关于使用此方法的好处是,如果您愿意,可以压缩两个以上的列表。由于Perl没有元组类型的概念,它几乎就像一个扁平化操作。

#3


5  

Although this specific function already exists in List::MoreUtils, you can use prototypes to give your own array functions the appearance of built-in array operators (like push, shift, pop):

虽然List :: MoreUtils中已存在此特定函数,但您可以使用原型为您自己的数组函数提供内置数组运算符(如push,shift,pop)的外观:

sub zipper (++) {  # perldoc perlsub
  my ($k, $v) = @_;
  die "Arrays must be equal length" if @$k != @$v;
  my $i;
  return map { $k->[$i++], $_ } @$v
}

%hash = zipper @keys, @values;
%hash = zipper \@keys, \@values;
%hash = zipper $key_aref, $value_aref;

#4


4  

You want to use List::MoreUtils ( or List::AllUtils ) to gain access to mesh aka zip see https://metacpan.org/pod/List::MoreUtils#mesh-ARRAY1-ARRAY2-ARRAY3

您想使用List :: MoreUtils(或List :: AllUtils)获取对网格的访问权限,请参阅https://metacpan.org/pod/List::MoreUtils#mesh-ARRAY1-ARRAY2-ARRAY3

#1


24  

Others have given good answers for mesh/zip side of the question, but if you are just creating a hash from an array of keys and one of values you can do it with the under-appreciated hash slice.

其他人已经为问题的网格/ zip方面提供了很好的答案,但是如果你只是从一组键和一个值创建一个哈希,你可以用不充分的哈希切片来做。

#!/usr/bin/env perl

use strict;
use warnings;

my @keys   = qw/abel baker charlie dog easy fox/;
my @values = qw/a b c d e f/;

my %hash;
@hash{@keys} = @values;

use Data::Dumper;
print Dumper \%hash;

Addendum

附录

I got to thinking why one may choose one method over the other. I personally think that the slice implementation is as readable as the zip, but others may disagree. If you are doing this often, you may care about speed, in which case the slice form is faster.

我开始思考为什么人们可以选择一种方法而不是另一种方法。我个人认为切片实现与zip一样可读,但其他人可能不同意。如果你经常这样做,你可能会关心速度,在这种情况下切片形式更快。

#!/usr/bin/env perl

use strict;
use warnings;

use List::MoreUtils qw/zip/;
use Benchmark qw/cmpthese/;

my @keys   = qw/abel baker charlie dog easy fox/;
my @values = qw/a b c d e f/;

cmpthese( 100000, {
  zip => sub {
    my %hash = zip @keys, @values;
  },
  slice => sub {
    my %hash;
    @hash{@keys} = @values;
  },
});

results:

结果:

         Rate   zip slice
zip   51282/s    --  -34%
slice 78125/s   52%    --

#2


11  

Since you offered a CPAN idea, there is List::MoreUtils and zip.

由于您提供了CPAN的想法,因此有List :: MoreUtils和zip。

use List::MoreUtils qw(zip);

my @keys   = qw/abel baker charlie dog easy fox/;
my @values = qw/a b c d e f/;

my @zipped = zip @keys, @values;

The contents of @zipped would be:

@zipped的内容是:

abel, a, baker, b, charlie, c, dog, d, easy, e, fox, f

The nice part about using this method is you can zip more than two lists if you wish. Since Perl has no concept of a tuple type, it is almost like a flattening operation.

关于使用此方法的好处是,如果您愿意,可以压缩两个以上的列表。由于Perl没有元组类型的概念,它几乎就像一个扁平化操作。

#3


5  

Although this specific function already exists in List::MoreUtils, you can use prototypes to give your own array functions the appearance of built-in array operators (like push, shift, pop):

虽然List :: MoreUtils中已存在此特定函数,但您可以使用原型为您自己的数组函数提供内置数组运算符(如push,shift,pop)的外观:

sub zipper (++) {  # perldoc perlsub
  my ($k, $v) = @_;
  die "Arrays must be equal length" if @$k != @$v;
  my $i;
  return map { $k->[$i++], $_ } @$v
}

%hash = zipper @keys, @values;
%hash = zipper \@keys, \@values;
%hash = zipper $key_aref, $value_aref;

#4


4  

You want to use List::MoreUtils ( or List::AllUtils ) to gain access to mesh aka zip see https://metacpan.org/pod/List::MoreUtils#mesh-ARRAY1-ARRAY2-ARRAY3

您想使用List :: MoreUtils(或List :: AllUtils)获取对网格的访问权限,请参阅https://metacpan.org/pod/List::MoreUtils#mesh-ARRAY1-ARRAY2-ARRAY3