Perl Array join() Function

How to Use "join()" Functions in Perl for Beginning Programmers

Man working on laptop
Brand X Pictures/Stockbyte/Getty Images

The Perl programming language 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 code, 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 programming 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.

Format
mla apa chicago
Your Citation
Brown, Kirk. "Perl Array join() Function." ThoughtCo, Feb. 13, 2018, thoughtco.com/perl-array-join-function-quick-tutorial-2641160. Brown, Kirk. (2018, February 13). 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 April 22, 2018).