perl function join
The join
function in Perl is used to concatenate elements of a list into a single string, separated by a specified delimiter. It takes two arguments: the delimiter and the list of elements to join.
Here's an example that demonstrates how to use join
:
#!/usr/bin/perl use strict; use warnings; # Define a list of strings my @words = ("foo", "bar", "baz"); # Join the strings with a comma delimiter my $string = join(",", @words); # Print the result print "List: @words\n"; print "String: $string\n";Suorce:www.theitroad.com
In this example, we start by defining a list of three strings: foo
, bar
, and baz
. We then use the join
function to concatenate the strings with a comma delimiter, resulting in a single string "foo,bar,baz"
. We assign this string to a variable called $string
.
We then print both the original list of words and the joined string to the console using print
.
Note that the delimiter specified to join
can be any string, not just a single character. Also note that join
does not add a delimiter after the last element in the list, so there will be no trailing delimiter in the resulting string.