Syntax
The syntax of the join function is mentioned below.
join(String, List)
The first argument of the join() function contains the character or the string value that will be used to combine the list values. The second argument of the join() function contains the list of string values or the array values that will be used for joining. This function returns the combined string after joining the list values with any character or string or the empty string.
Example-1: Join the List of String Values
Create a PERL file with the following code that shows the uses of the join() function for combining the list of string values. Three join() functions have been used in the code. The first join() function has been used to combine the string values with the empty string. The second join() function has been used to combine the string values with a space. The third join() function has been used to combine the string values with a colon(:). The output of each join() function has been printed later.
#Join strings with empty string
$combinedString = join("", "I", "like", "PERL");
print "The combined string value is:\n", $combinedString, "\n";
#Join strings with space
$combinedString = join(" ", "I", "like", "PERL");
print "The combined string value is:\n", $combinedString, "\n";
#Join strings with colon(:)
$combinedString = join(":", "I", "like", "PERL");
print "The combined string value is:\n", $combinedString, "\n";
Output:
The following output will appear after executing the above code.
Example-2: Join the Array Values
Create a PERL file with the following code that shows the uses of the join() function for combining the array values. The four join() functions have been used in the code. The first join() function has been used to combine the array values with the space. The second join() function has been used to combine the array values with the tab space. The third join() function has been used to combine the array values with a newline(\t). The fourth join() function has been used to combine the array values with a string value. The output of each join() function has been printed later.
@fruits = ("Mango", "Banana","Grape", "Lichi", "Strawberry");
#Join the array values with space
$combinedString = join( " ", @fruits);
print "The combined array values with space :\n", $combinedString, "\n";
#Join strings with tab space
$combinedString = join( "\t", @fruits);
print "The combined array values with tab :\n", $combinedString, "\n";
#Join strings with the newline
$combinedString = join( "\n", @fruits);
print "The combined array values with the newline :\n", $combinedString, "\n";
#Join strings with the string
$combinedString = join( " and ", @fruits);
print "The combined array values with the string :\n", $combinedString, "\n";
Output:
The following output will appear after executing the above code.
Example-3: Join the Values of Multiple Variables
Create a PERL file with the following code that shows the uses of the join() function for combining the values of multiple variables. Three input values will be taken from the user and stored in the variables, $name, $email, and $password. The join() function has been used in the code to combine the values of these variables with a colon(:). Next, the split() function was used to extract the values of the combined string and compare them with the particular email and password values. The welcome message will be printed if the “if” condition returns true; otherwise, an error message will be printed.
print "Enter your name: ";
#Take directory name from the user
$name = <> ;
chomp($name);
print "Enter your email: ";
#Take directory name from the user
$email = <> ;
chomp($email);
print "Enter your password: ";
#Take directory name from the user
$password = <> ;
chomp($password);
#Join strings with empty string
$combinedString = join( ":", $name, $email, $password);
print "The combined string value is:\n", $combinedString, "\n";
#Extract the combined string
@splittedValue = split(":", $combinedString);
if($splittedValue[1] eq '[email protected]' && $splittedValue[2] eq 'secret')
{
#Print welcome message
print "Welcome $splittedValue[0] to our site.\n";
}
else
{
#Print error message
print "Email or password is wrong.\n";
}
Output:
The following output would appear after executing the above code if the wrong email address was provided by the user.
The following output would appear after executing the above code if the valid email address and password were provided by the user.
Example-4: Join the Content of Multiple Files
Create a PERL file with the following code that shows the uses of the join() function for combining the content of multiple files. Two existing files have been opened by the open() function here. Next, the content of both files is stored in two arrays, and the join() function is used to combine the content of these array values.
# Set the first filename
$filename1 = 'myfile.txt';
# Open the file for reading
open $file_handler1, '<', $filename1 or die "Unable to open $filename1 file.";
# Store the first file content into an array
@file1 = <$file_handler1>;
# Set the second filename
$filename2 = 'temp.txt';
# Open the file for reading
open $file_handler2, '<', $filename2 or die "Unable to open $filename2 file.";
# Store the second file content into an array
@file2 = <$file_handler2>;
# Combine the file content using join() function
print join("", @file1, @file2);
Output:
The following output will appear after executing the above code. Here, the top three lines of the output are the content of the myfile.txt file, and the last line of the output is the content of the temp.txt file.
Conclusion
The purposes of using the join() function in PERL have been explained in this tutorial by using multiple PERL examples. I hope this tutorial will help the PERL users to use the join() function in their code properly.