Perl Array join() Function

Use the array join() function to combine elements of a list into a single string

Format
mla apa chicago
Your Citation
Brown, Kirk. "Perl Array join() Function." ThoughtCo, Dec. 5, 2016, thoughtco.com/perl-array-join-function-quick-tutorial-2641160. Brown, Kirk. (2016, December 5). Perl Array join() Function. Retrieved from https://www.thoughtco.com/perl-array-join-function-quick-tutorial-2641160 Brown, Kirk. "Perl Array join() Function." ThoughtCo. https://www.thoughtco.com/perl-array-join-function-quick-tutorial-2641160 (accessed September 20, 2017).
Man working on laptop
Brand X Pictures/Stockbyte/Getty Images

The Perl join() function is used to connect all the elements of a specific list or array into a single string using a specified joining expression. The list is concatenated into one string with the specified joining element contained in between each item. The syntax for the join() function is: join EXPR, LIST.

Join() Function at Work

In the following example, EXPR uses three different values. In one, it is a hyphen.

In one it is nothing, and in one it is a comma and a space.

#!/usr/bin/perl

$string = join( "-", "red", "green", "blue" );

print"Joined String is $string\n";

$string = join( "", "red", "green", "blue" );

print"Joined String is $string\n";

$string = join( ", ", "red", "green", "blue" );

print"Joined String is $string\n";

When the code is executed, it returns the following:

Joined String is red-green-blue

Joined String is redgreenblue

Joined String is red, green, blue

The EXPR is only placed between pairs of elements in LIST. It is not placed before the first element or after the last element in the string. 

About Perl

Perl, which is an interpreted language, not a compiled language, was a mature programming language long before the web, but it became popular with website developers because most of the content on the Web happens with text, and Perl is designed for text processing. Also, Perl is friendly and offers more than one way to do most things with the language.