Perl | join() Function

join() function in Perl combines the elements of LIST into a single string using the value of VAR to separate each element. It is effectively the opposite of split.

Note that VAR is only placed between pairs of elements in the LIST; it will not be placed either before the first element or after the last element of the string. Supply an empty string rather than undef, to join together strings without a separator.

Syntax: join(VAR, LIST)

Parameters:

  • VAR : Separator to be placed between list elements.
  • LIST : LIST to be converted into String.

Return: Returns the joined string

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Joining string with a separator
$string = join( "-", "Geeks", "for", "Geeks" );
print"Joined String is $string\n";
  
# Joining string without a separator
$string = join( "", "Geeks", "for", "Geeks" );
print"Joined String is $string\n";

chevron_right



Output:

Joined String is Geeks-for-Geeks
Joined String is GeeksforGeeks

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Joining string with '~' separator
$string = join( "~", "Geeks", "for", "Geeks" );
print"Joined String is $string\n";
  
# Joining string with '***' separator
$string = join( "***", "Geeks", "for", "Geeks" );
print"Joined String is $string\n";

chevron_right


Output :

Joined String is Geeks~for~Geeks
Joined String is Geeks***for***Geeks


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.