我想通过取数组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;
}
输出量
$ ./zipper.pl
easy=e
dog=d
fox=f
charlie=c
baker=b
abel=a
我想知道我是否忽略了Perl中的一个内置函数,该函数会等效于zipper()。这将是程序的最内层循环,并且需要尽快运行。如果没有内置或CPAN模块,有人可以改进我的实现吗?
最佳答案
其他人为该问题的网格/拉链方面给出了很好的答案,但是如果您只是根据一组键和一个值创建散列,则可以使用未被充分理解的hash slice来实现。
#!/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;
附录
我不得不思考为什么一个人可能会选择一种方法而不是另一种方法。我个人认为slice实现与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;
},
});
结果:
Rate zip slice
zip 51282/s -- -34%
slice 78125/s 52% --
关于arrays - Perl-内置函数可以将两个数组“压缩”在一起?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16755642/