join

If you're exhausted by the many ways to use split, you can rest assured that join isn't nearly so complicated. We can over-simplify by saying that join, does the inverse of split. If we said that, we would be mostly accurate. But there are no pattern matches going on. Join takes a string that specifies the delimiter to be concatenated between each item in the list supplied by subsequent parameter(s). Where split accommodates delimiters through a regular expression, allowing for different delimiters as long as they match the regexp, join makes no attempt to allow for differing delimiters. You specify the same delimiter for each item in the list being joined, or you specify no delimiter at all. Those are your choices. Easy.

To join a list of scalars together into one colon delimited string, do this:

 
$string = join ( ':', $last, $first, $phone, $age, $sex );
 
 

Whew, that was easy. You can also join lists contained in arrays:

 
$string = join ( ':', @array );
 

Use join to concatenate

It turns out that join is the most efficient way to concatenate many strings together at once; better than the '.' operator.

How do you do that? Like this:

 
$string = join ( '', @array );
[download]

As any good Perlish function should, join will accept an actual list, not just an array holding a list. So you can say this:

 
$string = join ( '*', "My", "Name", "Is", "Dave" );
 

Or even...

 
$string = join ( 'humility', ( qw/My name is Dave/ ) );
 
 

Which puts humility between each word in the list.

By specifying a null delimiter (nothing between the quotes), you're telling join to join up the elements in @array, one after another, with nothing between them. Easy.