Reputation: 11272
I am guaranteed to have a variable-length array with an odd number of elements. The first element will always be ignored. Every element thereafter needs to be separated into two respective arrays. For example:
Hello, a, 1, b, 2, c, 3
will result in the following two arrays: [a,b,c]
and [1,2,3]
.
Can I somehow use Perl's map/split function? Are there any pretty one-liners?
Upvotes: 0
Views: 943
Reputation: 26907
Looking at this it suddenly occurs to me that if you don't care about the order, you can misuse hash creation:
my %hash = @array[1 .. $#array];
(keys %hash, values %hash)
It also makes me wonder if there is an inverse for the Perl6 Zip operator?
Upvotes: 1
Reputation: 204678
@c = qw(Hello a 1 b 2 c 3);
@a = @c[map $_*2+1, 0 .. @c/2-1], @b = @c[map $_*2, 1 .. $#c/2];
# or
@a = @b = (), push @{$_%2 ? \@a : \@b}, $c[$_] for 1 .. $#c;
Upvotes: 1
Reputation: 98388
( [ map $array[$_*2-1], 1..($#array/2) ],
[ map $array[$_*2], 1..($#array/2) ] )
Upvotes: 4