The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Syntax::Operator::Zip - infix operator to compose two lists together

SYNOPSIS

On Perl v5.38 or later:

   use Syntax::Operator::Zip;

   foreach (@xvals Z @yvals) {
      my ($x, $y) = @$_;
      say "Value $x is associated with value $y";
   }

Or on Perl v5.14 or later:

   use v5.14;
   use Syntax::Operator::Zip qw( zip );

   foreach (zip \@xvals, \@yvals) {
      my ($x, $y) = @$_;
      say "Value $x is associated with value $y";
   }

DESCRIPTION

This module provides infix operators that compose lists of elements by associating successive elements from each of the input lists, forming a new list.

Support for custom infix operators was added in the Perl 5.37.x development cycle and is available from development release v5.37.7 onwards, and therefore in Perl v5.38 onwards. The documentation of XS::Parse::Infix describes the situation in more detail.

While Perl versions before this do not support custom infix operators, they can still be used via XS::Parse::Infix and hence XS::Parse::Keyword. Custom keywords which attempt to parse operator syntax may be able to use these.

Additionally, earlier versions of perl can still use the function-like wrapper versions of these operators. Even though the syntax appears like a regular function call, the code is compiled internally into the same more efficient operator internally, so will run without the function-call overhead of a regular function.

OPERATORS

Z

   my @result = @lhs Z @rhs;

   # returns  [$lhs[0], $rhs[0]], [$lhs[1], $rhs[1]], ...

Yields a list of array references, each containing a pair of items from the two operand lists. If one of the operand lists is shorter than the other, the missing elements will be filled in with undef so that every array reference in the result contains exactly two items.

   my @result = @alphas Z @betas Z @gammas Z ...

   # returns [$alphas[0], $betas[0], $gammas[0], ...], ...

Since version 0.10 this module supports list-associative combinations of more than two input lists at once. The result will be composed of parallel items from each of the given input lists.

M

   my @result = @lhs M @rhs;

   # returns  $lhs[0], $rhs[0], $lhs[1], $rhs[1], ...

Yields a list of the values from its operand lists, rearranged into pairs and flattened. If one of the operand lists is shorter than the other, the missing elements will be filled in with undef so that the result is correctly lined up.

   my @result = @alphas M @betas M @gammas M ...

   # returns $alphas[0], $betas[0], $gammas[0], ..., $alphas[1], ...

Since version 0.10 this module supports list-associative combinations of more than two input lists at once. The result will be composed of parallel items from each of the given input lists.

The result of this operator is useful for constructing hashes from two lists containing keys and values

   my %hash = @keys M @values;

This is also useful combined with the multiple variable foreach syntax of Perl 5.36 and above:

   foreach my ( $alpha, $beta, $gamma ) ( @alphas M @betas M @gammas ) {
      ...
   }

FUNCTIONS

As a convenience, the following functions may be imported which implement the same behaviour as the infix operators, though are accessed via regular function call syntax. The lists for these functions to operate on must be passed as references to arrays (either named variables, or anonymously constructed by [...]).

zip

   my @result = zip( \@lhs, \@rhs, ... );

A function version of the "Z" operator.

See also "zip" in List::Util.

mesh

   my @result = mesh( \@lhs, \@rhs, ... );

A function version of the "M" operator.

See also "mesh" in List::Util.

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>