in reply to Joining array with elements of another array

Another, albeit clumsy, way is to use List::MoreUtils
use warnings; use strict; use List::MoreUtils qw(mesh); my @arrOfJoiners = ('!','*','?'); my @arrToJoin = (1,2,3,4); my @arr2 = mesh(@arrToJoin, @arrOfJoiners); pop @arr2; print join('', @arr2), "\n"; __END__ 1!2*3?4

Maybe the source code for mesh will give you other ideas.

Replies are listed 'Best First'.
Re^2: Joining array with elements of another array
by davido (Cardinal) on Mar 18, 2013 at 16:07 UTC

    If the list is small enough that one doesn't mind the implicit temporary copy of joiners, this might seem a little more elegant, and still uses mesh (otherwise known as zip.

    use List::MoreUtils qw(zip); my @arrOfJoiners = ('!','*','?'); my @arrToJoin = (1,2,3,4); my $string = join '', zip @arrToJoin, @{[ @arrOfJoiners, '' ]}; print "$string\n";

    If the list of joiners is potentially huge, it would be better just to push an empty string to the end of the array:

    use List::MoreUtils qw(zip); my @arrOfJoiners = ('!','*','?'); my @arrToJoin = (1,2,3,4); push @arrOfJoiners, ('') x ( @arrToJoin - @arrOfJoiners ); my $string = join '', zip @arrToJoin, @arrOfJoiners; print "$string\n";

    Dave