I have two arrays:
@foo = (1, 2, 3, 4);
@bar = ('A', 'B', 'C', 'D');
If I zip them with mesh
/zip
from List::MoreUtils
, I get this:
@zipped = (1, 'A', 2, 'B', 3, 'C', 4, 'D');
How can I do this operation backwards, i.e. starting from @zipped
how can I get @foo
and @bar
?
List::Util::pairs.
use List::Util 'pairs';
my @zipped = ('1', 'A', '2', 'B', '3', 'C');
my ($foo, $bar) = pairs @zipped;
$foo
and $bar
will be references to arrays containing ('1'..'3')
and ('A'..'C')
, respectively.
Or if there are more than two arrays, use List::MoreUtils::part:
use List::MoreUtils 'part';
my @zipped = ('1', 'A', 'a', '2', 'B', 'b', '3', 'C', 'c');
my $number_of_arrays = 3;
my $i = 0;
my @arrayrefs = part { $i++ % $number_of_arrays } @zipped;
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