[tpm] how to merge/interleave two arrays?
ejanev at gmail.com
Wed Jan 18 10:55:09 PST 2012
I don't think there is a built in. Closest to it would be List::MoreUtils's
"zip()". From the doc (
http://search.cpan.org/~adamk/List-MoreUtils-0.33/lib/List/MoreUtils.pm ) :
mesh ARRAY1 ARRAY2 [ ARRAY3 ... ]zip ARRAY1 ARRAY2 [ ARRAY3 ... ]
Returns a list consisting of the first elements of each array, then the
second, then the third, etc, until all arrays are exhausted.
@x = qw/a b c d/;
@y = qw/1 2 3 4/;
@z = mesh @x, @y; # returns a, 1, b, 2, c, 3, d, 4
@a = ('x');
@b = ('1', '2');
@c = qw/zip zap zot/;
@d = mesh @a, @b, @c; # x, 1, zip, undef, 2, zap, undef, undef, zot
zip is an alias for mesh.
On Wed, Jan 18, 2012 at 1:43 PM, Fulko Hew <fulko.hew at gmail.com> wrote:
> Is there an easy way to merge/interleave two arrays (via a built-in)?
> I haven't found it. I.e.
> [1,4,2,5,3,6] = merge([1,2,3], [4,5,6]);
> toronto-pm mailing list
> toronto-pm at pm.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the toronto-pm