Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Joining array with elements of another array

by davido (Cardinal)
on Mar 18, 2013 at 16:07 UTC ( [id://1024096]=note: print w/replies, xml ) Need Help??


in reply to Re: Joining array with elements of another array
in thread Joining array with elements of another array

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1024096]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-03-29 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found