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. 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.
Check my new How To Tutorial eBooks (PDF format):to see a lot of fully commented examples that help you use the Perl statements and the Perl buit-in functions in your scripts.
| 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:
#join.pl
#!/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 join.pl script as follows:C:\Perl\bin>perl join.pl "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 join.pl script as follows:
C:\Perl\bin>perl join.pl C:\Perl Functions C:\Perl Statements
you’ll get the output:C:\Perl|Functions|C:\Perl|Statements
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.