Perl Variables  «Prev  Next»
Lesson 11Join Function in Perl
Objective Perl Join Function to format arrays

Perl Join Function

The join and split functions are tremendously useful for working with arrays.
You will find that you use these two functions quite often as a convenient method of formatting arrays for both display and storage.
In this lesson, we will examine the use of join, and in the next we will look at the use of split.
We have already used the join function in the array examples in the previous lessons, but here I will give you a more formal definition of the syntactical structure:
join separator, LIST
The separator is any scalar, and the LIST is any list of scalars (including an array).
A common use of this function is to print a comma-separated list of an array:
@array = ("Black", "Brown", "Polar", "Grizzly");
print join(', ', @array), "\n";

When run, you will get this output:

Black, Brown, Polar, Grizzly


Use the join function to format arrays in Perl

In Perl, the `join` function is used to concatenate the elements of an array into a single string, with a specified separator between each element. Here's the syntax and a few examples to help you understand how to use the `join` function:
Syntax
$string = join(separator, @array);

  • separato: The string that you want to place between each element of the array.
  • @array: The array whose elements you want to join into a single string.

Example 1: Basic Usage
#!/usr/bin/perl
use strict;
use warnings;

my @words = ("apple", "banana", "cherry");
my $fruit_string = join(", ", @words);

print "$fruit_string\n";  # Output: apple, banana, cherry

Example 2: Joining Numbers
#!/usr/bin/perl
use strict;
use warnings;

my @numbers = (1, 2, 3, 4, 5);
my $number_string = join("-", @numbers);

print "$number_string\n";  # Output: 1-2-3-4-5


Example 3: Joining with No Separator
If you don't want any separator between the elements, you can use an empty string as the separator:
#!/usr/bin/perl
use strict;
use warnings;

my @chars = ('H', 'e', 'l', 'l', 'o');
my $word = join("", @chars);

print "$word\n";  # Output: Hello

Example 4: Joining with a Complex Separator
You can use any string as the separator, including spaces and special characters:
#!/usr/bin/perl
use strict;
use warnings;

my @items = ("apple", "banana", "cherry");
my $list = join(" and ", @items);

print "$list\n";  # Output: apple and banana and cherry

Putting it All Together
Here's a complete Perl script demonstrating the use of `join` with different arrays and separators:
#!/usr/bin/perl
use strict;
use warnings;

my @fruits = ("apple", "banana", "cherry");
my $fruit_string = join(", ", @fruits);
print "$fruit_string\n";  # Output: apple, banana, cherry

my @nums = (10, 20, 30, 40, 50);
my $num_string = join(" - ", @nums);
print "$num_string\n";  # Output: 10 - 20 - 30 - 40 - 50

my @letters = ('a', 'b', 'c', 'd');
my $letter_string = join("", @letters);
print "$letter_string\n";  # Output: abcd

my @words = ("Perl", "is", "fun");
my $sentence = join(" ", @words);
print "$sentence\n";  # Output: Perl is fun
Using `join`, you can easily format arrays into strings with the desired separators in Perl.


Function Calls in Perl
Even though the parenthesis are not required for built-in functions in Perl, it's a good idea to use them when the function call is not the only element in the statement. Otherwise (due to the different leftward and rightward precedence) you may not always get the result you want.
For example, if you remove the parenthesis from the join in the above code, the "\n" becomes part of the list being passed to join, so you will get this output instead:
Black, Brown, Polar, Grizzly
Another common usage for join is to delimit an array for storage in a flat file. Later the delimited data can be read back into an array with split.
Here's an example:
@array = ("Black", "Brown", "Polar", "Grizzly");
print join(':', @array), "\n";

That prints the array with colons separating the elements like this:
Black: Brown: Polar: Grizzly 

To put them back into an array, you can use the split function.

SEMrush Software