Updated April 5, 2023
Introduction to Perl join array
In Perl, join is a function that is defined as a function for joining the elements or items in the array or a set of elements that are specified along with a joining expression that is given in the function to join all these elements into a single string. In general, the join() function in Perl is defined as a function for combining all the elements in the given array or list of elements into a single string which uses the separator to be replaced and returns only one string from the given array of elements which means an array is converted to a single array.
Working of join() Function on array in Perl with Examples
In this article, we will see the join() function for joining a specified array along with a given separator which that is used to separate each element in the array (these separators are only placed between the elements and not before the first element and not after the last element) and can be replaced to other given separator to make the given array into one single string in Perl programming language.
Therefore, this join() function is used for concatenating the given array having the list of elements into one single string along with using separators between elements. This function can also be used to append the elements in the given array into a single string. This join() function in Perl works completely opposite to the split() function as this breaks the given single string into an array of elements along with specified separators in Perl.
Syntax:
Join( SEP_EXPR, LST);
Parameters:
- SEP_EXPR: This parameter is used for specifying the separator in the given array or list, and this separator can also be any string that can be replaced with the separator in the given array or list, which is generally used as a joining element in the given array to convert it into one string.
- LST: This parameter is used for specifying the array or list of elements that need to be converted to a single string using this join() function.
The above join() function syntax returns the only one string that looks like the joined string of all the elements in the array or list of elements that is specified in the LST parameter.
Example #1
Code:
#!/usr/bin/perl
print "Demonstration of Join() function in Perl";
print "\n";
print "\n";
@arr = ('Educba ', 'Institute ', 'Bangalore ');
$sep = '-';
print "The given array is as follows:";
print "\n";
print @arr;
print "\n";
print "The given separator to join the elements in the given array to one string is :";
print "\n";
print $sep;
print "\n";
$str1 = join( $sep, @arr );
print " The concatenated string from the given array is ";
print "\n";
print $str1;
print "\n";
Output:
In the above program, we can see we have first declared an array with a variable named “@arr”, which has 3 elements, and each element has a space character also at the end of each element. Then we have specified a separator in the variable “$sep” as “-”, which is used as a joining element for converting the given array into one string. Then we are printing the specified array and separator. Then we are using the join() function and storing the result of the join() function in the variable “$str1” where we are passing the given array and separator to this function, and it returns only one string such as “’Educba -Institute –Bangalore”. We can see as we had the element with space appended at the end of each element, and hence the separator given is joined after space so that we can see in the output in the above screenshot.
We will see an example where there is no separator specified in the join() function and a string as a separator.
Example #2
Code:
#!/usr/bin/perl
print "Demonstration of Join() function in Perl";
print "\n";
print "\n";
@arr = ('Educba', 'Institute', 'Technology', 'Bangalore');
$sep ="Perl";
print "The given array is as follows:";
print "\n";
print @arr;
print "\n";
print "The given separator in the string form to join the elements in the given array to one string is :";
print "\n";
print $sep;
print "\n";
$str1 = join( $sep, @arr );
print " The concatenated string from the given array is using string as seperator ";
print "\n";
print $str1;
print "\n";
$str2 = join( "", @arr );
print " The concatenated string from the given array without seperator is ";
print "\n";
print $str2;
print "\n";
Output:
In the above program, we can see we have declared an array and sep in the variables declared as “@arr” and $sep”. Then we have printed the array and separator that is specified. Then we are using the join() function where it will return a single string such as “$str1” and “$str2” in the first string; we are using separator as a string give as “Perl”, which will be used in between the elements of the array and will result in “EducbaPerlInstitutePerlTechnologyPerlBangalore” and in the string $str2 we are having a string where no separator is given in the join() function and will return as “EducbaInstituteTechnologyBangalore”. Therefore we can see the output of the above code can be seen in the above screenshot.
We can also use the join() function on the numerical elements in the array in the below example.
Example #3
Code:
#!/usr/bin/perl
print "Demonstration of Join() function in Perl";
print "\n";
print "\n";
@arr = (1, 2, 3, 4);
$sep ="####";
print "The given array is as follows:";
print "\n";
print @arr;
print "\n";
print "The given separator to join the elements in the given array to one string is :";
print "\n";
print $sep;
print "\n";
$str1 = join( $sep, @arr );
print " The concatenated string from the given array is using string as separator ";
print "\n";
print $str1;
print "\n";
Output:
In the above code, we can see we have declared an array having a number as elements in the given array and separator as “####”, which is used for joining the elements of the array to one string. The output of the above code can be seen in the screenshot.
Conclusion
In this article, we conclude that the join() function in Perl is defined as a function for joining or combining the elements of the specified array or list of elements using the separator for joining these elements and converting them into one string. In this article, we saw the syntax and few examples of how the join() function works and how to specify the array and separator as a parameter to the function, and this function work opposite of the split() function in Perl.
Recommended Articles
This is a guide to Perl join array. Here we discuss the introduction and working of the join() function on array in Perl with examples. You may also have a look at the following articles to learn more –