Updated April 1, 2023
What is Split in Perl?
Split in Perl is used to split the string or expressions into fields by delimiter specified pattern by which we have used in split function. If we have not specified any pattern perl interpreter automatically takes a white space as the default. We have used limit to restrict the number of output returns from split function, limit is optional parameter that have used in split function in perl language. If we define negative value of limit then split function will consider as no limit and returns all the splitted data as a result.
Syntax and Parameter
Here we discuss the syntax and parameters:
Syntax:
Below is the syntax of split function in perl are as follows.
- Split; (Split function is used to split a string.)
- Split (Split function is used to split a string into substring) /pattern/ (Pattern is used to split string into substring.)
- Split (Using split function divide string into substring) /pattern/ (We have used pattern to split string into substring.), Expression (Expression name used to split string.)
- Split (Split function divide string into substring) /pattern/ (Based on pattern we have split string into substring.), Expression (Expression used to split the string.), Limit (Limit is used to restrict the number of output return using split function in Perl.)
Parameter:
Below is the parameter description of above syntax are as follows.
- Split: We have used a pattern, expression name in split function to divide any string into substrings. We have used limit in split function to restrict the number of the splitted output.
- Pattern: Pattern is used to divide the string into multiple substrings. Based on pattern we have divide string into substring. Pattern is most important and useful to divide string into substrings using split function in perl language.
- Expression: Expression is used to define variable name to string and display the output of the string.
- Limit: Limit is an optional parameter that was used in split function in Perl language. Using limit in split function we have restrict the number of output return using split function.
How Split Function Works in Perl?
Below is the working of split function in perl are as follows.
- This function will split a string into an array, string was splitted based on pattern which we have used in split function.
- Mainly we have used below parameter in split function are as follows.
- Expression
- Pattern
- Limit
- Pattern is used to in split function to divide string into multiple substrings.
- Based on pattern value which used we have divide string into substring. Pattern is most important and useful to divide string into substrings using split function in perl language.
- Expression is used to define variable name to string and display the output of the string. String output will display using expression in split function.
- Limit is used to restrict the number of output returns from split function.
- Limit is optional parameter in split function that have used in split function in perl language.
- If we define negative value of limit then split function will consider as no limit and returns all the splitted data as a result.
- We can split or divide string into following criteria are as follows.
- Single character
- Regular expression
- Group of character
- We can split string into no of way in perl language. We can split string using single character, regular expression and group of character.
Examples
Below some of the example are as follows.
1. Splitting on Character
Please find below example to split string using character. In the below example we have splitting string on character basis. We have splitting using comma. We have splitted number of character string into multiple sting.
Example:
use strict;
use warnings;
# Here we have used character as comma (,)
my $Char = 'AB, CD, , EF, GH, IJ, KL';
# using split() function and use pattern as comma.
my @Split = split(', ', $Char);
# displaying result using split function
print "@Split";
Output:
2. Splitting a String without Limit
Below example shows splitting string without limit are as follows. We have not used any limit while retrieving result of data by using split function in perl language.
Example:
use strict;
use warnings;
# String which is separated by !! Sign
my $Day = 'SUN!!MON!!TUE!!WED!!THU!!FRI!!SAT';
# using split function without Limit
my @Split = split('!!', $Day);
# displaying string after splitting and split divided string in new line
foreach my $WithoutLimit(@Split)
{
print "$WithoutLimit\n";
}
Output:
3. Splitting a String Using Limit
Below example shows splitting string using limit are as follows. We have used limit while retrieving result of data by using split function in perl language.
Example:
use strict;
use warnings;
# string which is separated by !! sign
my $Number = '1!!2!!3!!4!!5!!6!!7!!8!!9!!10';
# using split function with using Limit
my @Split = split('!!', $Number, 4); # We have used limit as 4.
# displaying string after splitting and split divided string in new line
foreach my $Limit (@Split)
{
print "$Limit\n";
}
Output:
4. Splitting on Undefined Values
The below example shows splitting string using undefined values are as follows.
Example:
use strict;
use warnings;
# string which is separated by using undefined value
my $Number = '12345678910';
# using split function with undef value
my @Split = split(undef, $Number);
# displaying string after splitting and split divided string in new line
foreach my $undef(@Split)
{
print "$undef\n";
}
Output:
5. Splitting on Space
The below example shows splitting string using space values are as follows.
Example:
use strict;
use warnings;
# string which is separated by using space value
my $Number = '1 2 3 4 5 6 7 8 9 10';
# using split function with undef value
my @Split = split(' ', $Number);
# displaying string after splitting and split divided string in new line
foreach my $space(@Split)
{
print "$space\n";
}
Output:
Conclusion
Split function is very important in perl language. Split function is used to split the string or expressions into fields by delimiter specified pattern by which we have used in split function. If we have not specified any pattern perl interpreter automatically takes a white space as the default.
Recommended Articles
We hope that this EDUCBA information on “Split in Perl” was beneficial to you. You can view EDUCBA’s recommended articles for more information.