Perl

Perl Split() Function

The split() function is used to divide any string based on any particular delimiter and if no delimiter is provided the space is used as the default delimiter. The delimiter can be a character, a list of characters, a regular expression pattern, the hash value, and an undefined value.  This function can be used in different ways by Perl script. Different uses of the split() function in Perl have been shown in this tutorial by using multiple examples.

Syntax

The split() can be used in different ways. Different syntaxes of split function are given below.

A. split
The split without any argument will split the value of the default variable based on the space.

B. split REGEX
The split function with regular expression only will split the value of the default variable on every match.

C. split REGEX, STRING
The split function with the regular expression and string value will split the value of the string based on the pattern.

D. split REGEX, STRING, LIMIT
The split function with the regular expression, string, and limit will split the string value based on the pattern and the number of elements of the returned array will depend on the limit value.

Example 1: Split String Based on Space

Create a PERL file with the following script that will split a string value based on the space. A string of 3 words has been used here. The ‘for’ loop has been used to print the array values returned by the split() function.

#Define a text
$text = "PERL Programming Language";
#Split the text based on the space
@split_arr = split(' ', $text);

#Iterate each element of the array
foreach $word (@split_arr) {
    #Print each splitted word in each line
    print "$word\n";
}

Output
The following output will appear after executing the above script. The returned array contains three elements which are printed here.

Example 2: Split String Based on the Particular Character

Create a PERL file with the following script that will split a string value based on the character, ‘:’. A string with 3 ‘;’ has been used here. Each element of the array returned by the split() function has been printed separately by using the index.

#Define a text
$text = '11876:Mossaraf Karim:CSE';

#Split the text based on ':'
@split_arr = split(':', $text);

#Print each array values
print "ID: $split_arr[0]\n";
print "Name: $split_arr[1]\n";
print "Department: $split_arr[2]\n";

Output
The following output will appear after executing the above script. The string value has been divided into three words based on the ‘:’.

Example 3: Split String with the Limit

Create a PERL file with the following script that will split a string value based on the character, ‘,’ and with the limit value. A string with 6 ‘,’ has been used and the limit value has been set to 5 here. Each element of the array returned by the split() function has been printed separately by using the index. The ‘for’ loop has been used to print the array values returned by the split() function.

#Define a text
$colors = " Red, Green, Blue, White, Black, Yellow, Cyan";
#Split the text based on the character and limit
@split_arr = split(',', $colors, 5);

print "The colors are:\n";
#Iterate each element of the array
foreach $word (@split_arr) {
    #Print each splitted word in each line
    print "$word\n";
}

Output
The following output will appear after executing the above script.  The string value has been divided into five words based on the ‘,’ and the limit value 5.

Example 4: Split String with the Regular Expression 

Create a PERL file with the following script that will split a string value based on the regular expression. A string with alphabetic characters and numbers has been used here. The pattern ‘/+d/’ has been used to split the string based on the digit. The pattern ‘/+D/’ has been used to split the string based on alphabets. The ‘for’ loop has been used to print the array values returned by the split() function.

#Define a text
$text = "I like69perl50programming";

#Split string based on the digits
@words = split(/\d+/, $text);

print "The output after splitting string based on digits:\n";
#Iterate the array values
foreach $word (@words)
{
    #Print the each value
    print "$word ";
}
#Add newline
print "\n";

#Split string based on the characters
@words = split(/\D+/, $text);

print "The output after splitting string based on characters:\n";
#Iterate the array values
foreach $word (@words)
{
    #Print the each value
    print "$word ";
}

Output
The following output will appear after executing the above script. The first split() function has returned an array of three elements which are ‘I like’, ‘perl’, and ‘programming’. The second split() function has returned an array of two elements which are ‘69’ and ‘50’.

Example 5: Split String Based on an Undefined Value

Create a PERL file with the following script that will split a string value based on the undefined value. A string of 11 characters has been used here. The ‘undef’ keyword is used as the pattern value of the split() function that will split the string based on each character of the string. The ‘for’ loop has been used to print the array values returned by the split() function.

#Define a string
$text = 'Hello World';

print "The main string value:\n";
print "$text";

#Split string based on undefined value
@split_arr = split(undef, $text);

print "\nThe splitted string value:\n";

#Iterate each element of the array
foreach $val (@split_arr) {
    #Print each splitted word in each line
    print "$val ";
}
print "\n";

Output
The following output will appear after executing the above script. The returned array contains each character of the string as an array element.

Conclusion

The various uses of the split() function have been shown in this tutorial by using different PERL scripts. The purposes of using regular expression patterns and limit in the split() function have been explained properly in this tutorial for helping the PERL users use this function efficiently in the script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.