The cut up() operate is used to divide any string primarily based on any specific delimiter and if no delimiter is offered the house is used because the default delimiter. The delimiter generally is a character, an inventory of characters, an everyday expression sample, the hash worth, and an undefined worth.  This operate can be utilized in numerous methods by Perl script. Completely different makes use of of the cut up() operate in Perl have been proven on this tutorial through the use of a number of examples.

Syntax

The cut up() can be utilized in numerous methods. Completely different syntaxes of cut up operate are given under.

A. cut up
The cut up with none argument will cut up the worth of the default variable primarily based on the house.

B. cut up REGEX
The cut up operate with common expression solely will cut up the worth of the default variable on each match.

C. cut up REGEX, STRING
The cut up operate with the common expression and string worth will cut up the worth of the string primarily based on the sample.

D. cut up REGEX, STRING, LIMIT
The cut up operate with the common expression, string, and restrict will cut up the string worth primarily based on the sample and the variety of components of the returned array will rely on the restrict worth.

Instance 1: Cut up String Based mostly on House

Create a PERL file with the next script that can cut up a string worth primarily based on the house. A string of three phrases has been used right here. The ‘for’ loop has been used to print the array values returned by the cut up() operate.

#Outline a textual content
$textual content = “PERL Programming Language”;
#Cut up the textual content primarily based on the house
@split_arr = split(‘ ‘, $textual content);

#Iterate every aspect of the array
foreach $phrase (@split_arr) {
    #Print every splitted phrase in every line
    print “$phrasen;
}

Output
The next output will seem after executing the above script. The returned array accommodates three components that are printed right here.

Instance 2: Cut up String Based mostly on the Explicit Character

Create a PERL file with the next script that can cut up a string worth primarily based on the character, ‘:’. A string with 3 ‘;’ has been used right here. Every aspect of the array returned by the cut up() operate has been printed individually through the use of the index.

#Outline a textual content
$textual content = ‘11876:Mossaraf Karim:CSE’;

#Cut up the textual content primarily based on ‘:’
@split_arr = split(‘:’, $textual content);

#Print every array values
print “ID: $split_arr[0]n;
print “Identify: $split_arr[1]n;
print “Division: $split_arr[2]n;

Output
The next output will seem after executing the above script. The string worth has been divided into three phrases primarily based on the ‘:’.

Instance 3: Cut up String with the Restrict

Create a PERL file with the next script that can cut up a string worth primarily based on the character, ‘,’ and with the restrict worth. A string with 6 ‘,’ has been used and the restrict worth has been set to five right here. Every aspect of the array returned by the cut up() operate has been printed individually through the use of the index. The ‘for’ loop has been used to print the array values returned by the cut up() operate.

#Outline a textual content
$colours = ” Purple, Inexperienced, Blue, White, Black, Yellow, Cyan”;
#Cut up the textual content primarily based on the character and restrict
@split_arr = split(‘,’, $colours, 5);

print “The colours are:n;
#Iterate every aspect of the array
foreach $phrase (@split_arr) {
    #Print every splitted phrase in every line
    print “$phrasen;
}

Output
The next output will seem after executing the above script.  The string worth has been divided into 5 phrases primarily based on the ‘,’ and the restrict worth 5.

Instance 4: Cut up String with the Common Expression 

Create a PERL file with the next script that can cut up a string worth primarily based on the common expression. A string with alphabetic characters and numbers has been used right here. The sample ‘/+d/’ has been used to separate the string primarily based on the digit. The sample ‘/+D/’ has been used to separate the string primarily based on alphabets. The ‘for’ loop has been used to print the array values returned by the cut up() operate.

#Outline a textual content
$textual content = “I like69perl50programming”;

#Cut up string primarily based on the digits
@phrases = split(/d+/, $textual content);

print “The output after splitting string primarily based on digits:n;
#Iterate the array values
foreach $phrase (@phrases)
{
    #Print the every worth
    print “$phrase “;
}
#Add newline
print n;

#Cut up string primarily based on the characters
@phrases = split(/D+/, $textual content);

print “The output after splitting string primarily based on characters:n;
#Iterate the array values
foreach $phrase (@phrases)
{
    #Print the every worth
    print “$phrase “;
}

Output
The next output will seem after executing the above script. The primary cut up() operate has returned an array of three components that are ‘I like’, ‘perl’, and ‘programming’. The second cut up() operate has returned an array of two components that are ‘69’ and ‘50’.

Instance 5: Cut up String Based mostly on an Undefined Worth

Create a PERL file with the next script that can cut up a string worth primarily based on the undefined worth. A string of 11 characters has been used right here. The ‘undef’ key phrase is used because the sample worth of the cut up() operate that can cut up the string primarily based on every character of the string. The ‘for’ loop has been used to print the array values returned by the cut up() operate.

#Outline a string
$textual content = ‘Whats up World’;

print “The primary string worth:n;
print “$textual content”;

#Cut up string primarily based on undefined worth
@split_arr = split(undef, $textual content);

print nThe splitted string worth:n;

#Iterate every aspect of the array
foreach $val (@split_arr) {
    #Print every splitted phrase in every line
    print “$val “;
}
print n;

Output
The next output will seem after executing the above script. The returned array accommodates every character of the string as an array aspect.

Conclusion

The assorted makes use of of the cut up() operate have been proven on this tutorial through the use of completely different PERL scripts. The needs of utilizing common expression patterns and restrict within the cut up() operate have been defined correctly on this tutorial for serving to the PERL customers use this operate effectively within the script.

Did you want this text?
Share it on any of the next social media channels under to offer us your vote. Your suggestions helps us enhance.

close