[tpm] toronto-pm Digest, Vol 58, Issue 11

Carlo Costantini carlo at carlocostantini.ca
Wed Jan 18 14:05:05 PST 2012


Not sure exactly what you want this for but you can do using hash slices:

http://www.perlmonks.org/?node_id=4402

#!/usr/bin/perl
#
my %c = ();
my @a = (1,2,3);
my @b = (4,5,6);

@c{@a} = @b;
@b = %c;
print @b;
#143625

If order needs to be preserved then you would have to pull it out of
the hash by the order of the keys.

If you just want to make the corresponding indices of the array match
to create hash, then a hash slice is your best friend.

On Wed, Jan 18, 2012 at 3:00 PM,  <toronto-pm-request at pm.org> wrote:
> Send toronto-pm mailing list submissions to
>        toronto-pm at pm.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>        http://mail.pm.org/mailman/listinfo/toronto-pm
> or, via email, send a message with subject or body 'help' to
>        toronto-pm-request at pm.org
>
> You can reach the person managing the list at
>        toronto-pm-owner at pm.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of toronto-pm digest..."
>
>
> Today's Topics:
>
>   1. how to merge/interleave two arrays? (Fulko Hew)
>   2. Re: how to merge/interleave two arrays? (Emil Janev)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Wed, 18 Jan 2012 13:43:31 -0500
> From: Fulko Hew <fulko.hew at gmail.com>
> To: toronto-pm at pm.org
> Subject: [tpm] how to merge/interleave two arrays?
> Message-ID:
>        <CAGuV3hOGkbE97HhVpXrmM2i3XUL4FhjZ0psd=O4Y9d9jj-8+6w at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> 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]);
>
> TIA
> Fulko
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20120118/045aa56e/attachment-0001.html>
>
> ------------------------------
>
> Message: 2
> Date: Wed, 18 Jan 2012 13:55:09 -0500
> From: Emil Janev <ejanev at gmail.com>
> To: Fulko Hew <fulko.hew at gmail.com>
> Cc: toronto-pm at pm.org
> Subject: Re: [tpm] how to merge/interleave two arrays?
> Message-ID:
>        <CAOGFQQnrs0KtGsdn9=sTCX7DqszGJLcC0hgEWM11BSPgQ-tuNw at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> Hi Fulko,
>
> 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.
>
> Examples:
>
>    @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]);
>>
>> TIA
>> Fulko
>>
>>
>> _______________________________________________
>> toronto-pm mailing list
>> toronto-pm at pm.org
>> http://mail.pm.org/mailman/listinfo/toronto-pm
>>
>>
>
>
> --
> Emil Janev
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20120118/71a4576e/attachment-0001.html>
>
> ------------------------------
>
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm
>
>
> End of toronto-pm Digest, Vol 58, Issue 11
> ******************************************


More information about the toronto-pm mailing list