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.
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.
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.
$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.
$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.
$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.