6.2 Join

With the function join you can glue array-items or list-items (at least 2 items) and a delimiter together into a string. It's the opposite of the function split. A few examples.

Example 1:


@arr = qw( a b c d e f );
$str = join(":", @arr);
print("$str"); # output: a:b:c:d:e:f
Example 2:

@arr = qw( f e d c b a );
$str = join(":", sort(@arr));
print("$str"); # output: a:b:c:d:e:f
Example 3:

@arr = qw( a b c d e f );
$str = join(" - ", @arr[0..2]);
print("$str"); # output: a - b - c
Example 4:

@arr = qw( a b c d e f );
$str = join(" - ", @arr[0..2], "suffix");
print("$str"); # output: a - b - c - suffix
Example 5:

@arr = qw( a b c d e f );
$str = join(" - ", "prefix", @arr[0..2], "suffix");
print("$str"); # output: prefix - a - b - c - suffix
Example 6:

$name1 = "Reinier";
$name2 = "Henri";
$str = join("::", ($name1, $name2));
print("$str"); # output: Reinier::Henri
Example 7:

$name1 = "Sebastian";
$name2 = "Daniel";
$str = join(" and ", ($name1, $name2), "Florence");
print("$str"); # output: Sebastian and Daniel and Florence
Example 8:

print(join(", ", 1..5)); # output: 1, 2, 3, 4, 5
Example 9:

print( join(" - ", split(/:/, "a:b:c")), " -> ", "somewhat of a detour", " :-)" ); # output: a - b - c -> somewhat of a detour :-)