I have such code in Perl:
#!/usr/bin/perl -w
my @a = ('one', 'two', 'three');
my @b = (1, 2, 3);
I want to see in result this: @c = ('one1', 'two2', 'three3');
Is there way I can merge these lists into one?
Assuming that you can guarantee the two arrays will always be the same length.
my @c = map { "$a[$_]$b[$_]" } 0 .. $#a;
As an alternative, you can use pairwise from List::MoreUtils:
#!/usr/bin/env perl
use strict;
use warnings;
use List::MoreUtils qw( pairwise );
my @a = ( 'one', 'two', 'three' );
my @b = ( 1, 2, 3 );
my @c = do {
no warnings 'once';
pairwise { "$a$b" } @a, @b;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With