Home
Perl How To
Perl Books
Perl Basics
Perl Editors
Perl Modules
Perl Database
Net Programming
Sitemap
About Me
Contact Me
Blog
 

Perl join Function



NEW!!!

Check my new resource:    Perl How To Tutorial eBooks



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
@perlFunc = ("substr","grep","defined","undef");
$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 parenthesis 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. We can rewrite the above example using the dot operator:

$perlFunc = "substr"." "."grep"." "."defined"." "."undef";
#or $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 represents 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 call 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 we run the script with the arguments "C:\Perl Functions" and "C:\Perl Statements", we 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, by 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).




NEW!!!

Do you want more information about the basic Perl topics?

Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :


Perl How To Tutorial eBooks


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
        Array Functions
    Functions in alphabetical order
        chomp
        chop
        chr
        crypt
        defined
        grep
        hex
        index
        join
        lc
        lcfirst
        length
        oct
        ord
        pack
        pop
        push
        reverse
        rindex
        shift
        split
        substr
        uc
        undef
        unshift

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!


footer for perl join page