Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Joining array with elements of another array

by Doctrin (Beadle)
on Mar 18, 2013 at 15:21 UTC ( #1024085=perlquestion: print w/ replies, xml ) Need Help??
Doctrin has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear Monks. I have this:
my @arrOfJoiners = ('!','*','?'); my @arrToJoin = (1,2,3,4);
I wanna have this: 1!2*3?4 Surely, I could make something like:
my @arrOfJoiners = ('!','*','?'); my @arrToJoin = (1,2,3,4); my $s; for my $arrElem (@arrToJoin) { $s .= $arrElem . (shift @arrOfJoiners); } print $s;
But, is there a smarter way to do it using, say, "join" ? Maybe, even one-liner?.. Thanks in advance. ====UPD==== Oh, looks like I got it:
$s = join "", (map {$_ . (shift @arrOfJoiners)} @arrToJoin);
Thanks, I think this would be fine.

Comment on Joining array with elements of another array
Select or Download Code
Re: Joining array with elements of another array
by Anonymous Monk on Mar 18, 2013 at 15:23 UTC

    Is there a smart way to do it using, say, "join" ? Thanks in advance.

    No, join is simple

Re: Joining array with elements of another array
by toolic (Chancellor) on Mar 18, 2013 at 15:49 UTC
    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.

      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

Re: Joining array with elements of another array
by tobyink (Abbot) on Mar 18, 2013 at 16:12 UTC

    Non-destructive; look ma, no List::MoreUtils...

    use v5.12; my @arrOfJoiners = ('!','*','?'); my @arrToJoin = (1,2,3,4); say join "", map(($arrToJoin[$_], $arrOfJoiners[$_]), 0..$#arrToJoin);
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1024085]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2014-12-21 08:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (104 votes), past polls