sub zip { my ($ref1, $ref2) = @_; my @zip; while (@$ref1 || @$ref2) { push @zip => shift @$ref1 || undef; push @zip => shift @$ref2 || undef; } return @zip; } my $x = 1; my $y = 2; my @a=qw(1 2 3); my @b=qw(7 8 9); my @zip = zip(\@a, \@b); while (my ($item, $element) = splice @zip => 0, 2) { print "$x: $item $y: $element\n"; } #### for zip(@a,@b) -> $item, $element { say "$x: $item $y: $element"; }