Perl join Function

These sites are FOR SALE:,!
If you are interested, please use my Contact page.

Perl join function is used to concatenate the elements of an array or a list into a string, using a separator given by a scalar variable value. In this short free tutorial I’ll show you the syntax form and a few examples about how to use it in your program applications.

The syntax form of Perl join function is as follows:

$string = join (EXPR, LIST);

As you can see, the join function has two parameters:
  • EXPR – may be any string and it represents a separator for the element of the list or array
  • LIST – represents a list or array whose elements will be merged into a string
The join function will return a string that contains the elements of the array or list, connected through a string separator.

Click Below to See the Best

Perl How-to Snippet Collection

and Save Hours of Surfing on Internet!

Perl join function is the opposite of the Perl split function: meanwhile join function concatenates the elements of a list into a string, split function breaks up a string into a list of words and returns that list.

Next, a simple example about how to use it:

#initialize an array
my @perlFunc = ("substr","grep","defined","undef");
my $perlFunc = join " ", @perlFunc;
print "Perl Functions: $perlFunc\n";

If you run it, you’ll get as output:

Perl Functions: substr grep defined undef

In the above example we used the space separator to glue the elements of the @perlFunc array. As you see, you can omit the parentheses when you call the Perl join function.

You can concatenate two or more strings, using the dot (.) operator. The main difference between the dot operator and the join function is that whether the dot operator has two operands (the strings that will be merged), Perl join function can use a separator and is called against the elements of a list or array. You can rewrite the above example using the dot operator:

my $perlFunc = "substr"." "."grep"." "."defined"." "."undef";
#or my $perlFunc .= $_." " for("substr","grep","defined","undef"); 
print "Perl Functions: $perlFunc\n";

The output will be the same as before.

I’ll show you now two other snippet code examples with the join function.

How to change the join separator

use strict;
use warnings;

my $string = "one two three four";

# split the string into an array using the space delimiter
my @array = split(/ /, $string);

# merges the array elements into the original string
# using the "--" delimiter
$string = join("--",@array);

# print the string value
print "New String: $string\n";

# it expects: New String: one--two--three--four

As you have noticed in the example above, you can use a string as a separator ("--") in order to join the elements of an array into a string. We used the split function to split the string into an array.

Convert @ARGV to a string variable

First, I want to remind you the @ARGV array, in a few words. This is a special array of Perl and is the list of arguments passed to a script that you run from a command line. In a command line the arguments are separated by space. $ARGV[0] will store the first argument, $ARGV[1] the second argument and so on.

If you want to find the number of the command-line arguments, you can invoke the size of the @ARGV array, i.e. $#ARGV+1 (you need to add 1 because $#ARGV actually represents the last subscript of the array, the first subscript being 0).

If you want, you can run the Perl script without arguments in the command line and initialize the @ARGV array across your Perl script.

In the following example we want to concatenate the command-line arguments into a string using the join function – the arguments will be separated by '|' delimiter. Our example is meant to be run on a Windows machine. We’ll analyze the case when the arguments can contain spaces. See the following example:
#!/usr/bin/perl -w 
use strict;

my $string = join('|',@ARGV);
print "$string\n";

If you run the script with the arguments "C:\Perl Functions" and "C:\Perl Statements", you will call the script as follows:

C:\Perl\bin>perl "C:\Perl Functions" "C:\Perl Statements"

which it will display:

C:\Perl Functions|C:\Perl Statements

Or if you want to initialize the @ARGV array inside the Perl script, you can simply insert the next line:

@ARGV = ("C:\Perl Functions", "C:\Perl Statements");

before the line that contains the join function, and run the script without arguments. It will produce the same output.

Please note that it is important to quote the arguments if they contain spaces, for instance if you’ll run the initial script as follows:

C:\Perl\bin>perl C:\Perl Functions C:\Perl Statements

you’ll get the output:


where you can see that Perl found 4 arguments in the command line (because of spaces).

If you want to download the Perl join script with all the above examples included, please click here: Script download

To see how to use the Perl join function with array of arrays, try this.


Through these exercises you have the opportunity to try yourself to write some script code where you can use the Perl join function. These exercises are completely covered in my Perl Functions for List Data where I show you how to play with this important function in detail.

1. Given the following array of arrays (@AoA):
my @AOA = ( 
  ['a1', 'b1', 1],
  ['a2', 'b2', 2],
  ['a3', 'b3', 3],
  ['a4', 'b4', 4],
use join to create a string from the inner array of index 1 (the second inner array). The resulting string must be: 'a2,b2,2'.
2. Given the same array of arrays as in the previous example, join into a string the elements of the second column of the AoA matrix. The resulting string must be: 'b1,b2,b3,b4'.
3. Given the same array of arrays as in the previous examples, turn each inner array into a string and get an array with all these strings. If you print the resulting array, you must get the following output:
4. Given the following array of hashes (@AoH):
my @AoH = (
       {name => 'John',  age => '23'},
       {color => 'blue'},
       {width => 95, height => 121, top => 23, left => 15}
extract all its keys and for each inner hash print them on the same line as in the following example:
age     name
height  left    top     width
For each referenced hash, the keys are listed in the inner order of the hash table.
5. Given the following hash of arrays (%HoA):
my %HoA = ( colors => ["red", "yellow", "blue"], 
            shapes => ["circle", "rectangle"], 
            flowers => ["rose", "tulip", "snowdrop"] 
print the hash of arrays as:
shapes: circle, rectangle
flowers: rose, snowdrop, tulip
colors: blue, red, yellow
where the hash keys are ordered in a descending ASCIIbetical order and the elements of the inner arrays are ordered in an ascending order.

A-N-Y-O-N-E Can Learn and Master Perl!
And That Includes YOU!

Check these how-to tutorial eBooks (PDF format):

Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        Regular Expressions and Pattern Matching
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order
        join (more)

return from Perl join Function to Perl Basics

Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!