Chap
Chap

Reputation: 3835

Perl - built-in function to "zipper" together two arrays?

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:

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

Upvotes: 18

Views: 4550

Answers (4)

optional
optional

Reputation: 2071

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

Upvotes: 4

squiguy
squiguy

Reputation: 33360

Since you offered a CPAN idea, there is List::MoreUtils and 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:

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.

Upvotes: 11

Joel Berger
Joel Berger

Reputation: 20280

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.

#!/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.

#!/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%    --

Upvotes: 25

Julian Fondren
Julian Fondren

Reputation: 5619

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

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;

Upvotes: 5

Related Questions