Javatpoint Logo
Javatpoint Logo

Perl split Function

The Perl split function splits a string into an array. A string is splitted based on delimiter specified by pattern. By default, it whitespace is assumed as delimiter.

split syntax is:


Perl split Example

In this example, split returns a list of strings. The delimiter is (-). It means string is splitted at (-).

Output:

		
Cu K Na Hg Pb Li

Perl split Limit number of parts

We can limit the number of parts to be splitted for a string.

In this example, string splits in 3 parts.

Output:

Cu K Na-Hg-Pb-Li

Perl split on Multiple Characters

We can split a character at more than one delimiter. In the following example, we have split the string at (=) and (,).

Output:

Vishal 18Sept Anu 11May Juhi 5Jul

As they are key-value pairs, we can assign the result into a hash instead of an array.

Output:

$VAR1 = (
		'Anu', => '11th May',
		'Vishal', => '18th Sept',
		'Juhi', => '5th Jul', 
);

Perl split on Empty String

Split on empty string means the string will split at every place where it will find an empty string. There is an empty string, between every two characters. It means it will return the original string split into individual characters.

Output:

J A V A P O I N T

Perl join Function

Perl join character, joins elements into a single string using a delimiter pattern to separate each element. It is opposite of split.

join syntax is:


Perl join Example

In the following example, we have joined in three different ways.

First, we are joining (:) in between each elements of array.

Second, we will join (- vowels) with the output of first one.

Third, we added two more characters in the original array.

Output:

A:E:I
A:E:I -vowels
AEIOU





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA