How can I make the reverse side of zip / mesh from List :: MoreUtils?

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 the following:

 @zipped = (1, 'A', 2, 'B', 3, 'C', 4, 'D'); 

How can I perform this operation backwards, i.e. starting with @zipped , how can I get @foo and @bar ?

+5
source share
1 answer

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; 
+8
source

Source: https://habr.com/ru/post/1213186/


All Articles