split()是Perl中的一个字符串函数,用于拆分,也可以说是将字符串切成较小的部分。分割字符串有不同的条件,例如在单个字符,正则表达式(模式),一组字符或未定义的值等上。关于此函数的最佳选择是,用户可以指定分割字符串的部分入。
用法:
split /Pattern/, Expression, Limit or split /Pattern/, Expression or split /Pattern/ or Split
在以上语法中,为Pattern指定了一个正则表达式,该正则表达式提供了分割字符串的条件。表达式是要分割的字符串。限制是一种限制,它可以停止在字符串中找到的第(n-1)个模式处进行拆分。
返回值:此方法在两个上下文中返回值,如下所示:
In Array Context: Here it returns a list of the fields which found in Expression. If no Expression is specified then it returns $_.
In Scalar Context: Here it returns the number of fields which found in Expression and then stored the fields in the @_ array.
使用split()函数的方式有以下几种:
- 分割角色
- 在字符串之间无限分割
- 在带限制的字符串之间分割
- 拆分未定义的值
- 在正则表达式(模式)上分割
- 散列拆分
- 在空间分裂
Splitting on a Character
用户可以将字符串拆分或分割为不同的字符,例如逗号(,)反斜杠(\)等。当您必须从另一个程序或文件中解析数据时,通常使用这种类型的拆分。不要使用split()来解析CSV(逗号分隔值)文件。如果您的数据中有逗号,请改用Text::CSV。
例:
# Perl program to demonstrate the splitting on character
#!/usr/bin/perl
use strict;
use warnings;
# Here character is comma(, )
my $str = 'Geeks, for, Geeks';
# using split() function
my @spl = split(', ', $str);
# displaying result using foreach loop
foreach my $i (@spl)
{
print "$i";
}
GeeksforGeeks
Splitting among String without any Limit
这也与角色拆分相同。字符串的数据用两个!!分隔。
例:
# Perl program to demonstrate the
# splitting among string without Limit
#!/usr/bin/perl
use strict;
use warnings;
# string which is seperated by !! sign
my $str = 'GFG!!Geeks!!55!!GeeksforGeeks';
# using split function without Limit
my @spl = split('!!', $str);
# displaying string after splitting
foreach my $i (@spl)
{
print "$i\n";
}
GFG Geeks 55 GeeksforGeeks
Splitting among String with Limit
这也与角色拆分相同。字符串的数据用两个!!分隔。在此,用户可以通过在split函数中传递第三个参数(该参数为正整数)来限制字符串将拆分为的部分的数量。在下面的示例中,用户将Limit设置为3,这样即使出现4次!!,也将限制字符串拆分为3!在字符串中。
例:
# Perl program to demonstrate the
# splitting on string with Limit
#!/usr/bin/perl
use strict;
use warnings;
# string which is seperated by !! sign
my $str = 'GFG!!Geeks!!55!!GeeksforGeeks';
# using split function with Limit
my @spl = split('!!', $str, 3);
# displaying string after splitting
foreach my $i (@spl)
{
print "$i\n";
}
GFG Geeks 55!!GeeksforGeeks
Splitting on an undefined value
如果用户尝试拆分未定义的值,则字符串将拆分为每个字符。
例:
# Perl program to demonstrate the
# splitting on undefined value
#!/usr/bin/perl
use strict;
use warnings;
# string to be split
my $str = 'GeeksforGeeks GFG';
# using split function
my @spl = split(undef, $str);
# displaying string after splitting
foreach my $i (@spl)
{
print "$i\n";
}
输出:
G e e k s f o r G e e k s G F G
运行时错误:
Use of uninitialized value in regexp compilation at /home/38ececda726bcb7e68fb7b41eee5b8d9.pl line 12.
Splitting on a Pattern or Regex
有时,用户可能希望在模式(正则表达式)或特定类型的字符上分割字符串。在这里,我们将使用特殊字符类来制作数字模式(整数),如下所示:
例:
# Perl program to demonstrate the
# splitting on a pattern(regex)
#!/usr/bin/perl
use strict;
use warnings;
# string to be split
my $str = 'Geeks1for2Geeks';
# using split function
# \d+ will match one or more
# integer numbers & placed
# between two //
my @spl = split(/\d+/, $str);
# displaying string after splitting
foreach my $i (@spl)
{
print "$i\n";
}
Geeks for Geeks
Splitting into a hash
用户可以将数据或字符串拆分为哈希而不是数组。本质上,哈希是键/值对。拆分之前,用户必须具有有关哈希的知识。
例:
# Perl program to demonstrate the
# splitting into the hash
#!/usr/bin/perl
use strict;
use warnings;
# hash to be split
my $has = 'GFG=1;GEEKS=2;PROGEEK=3';
# using split function
my %spl = split(/[=;]/, $has);
# after splitting displaying the values
foreach my $i (keys %spl)
{
print "$i:$spl{$i}\n";
}
GFG:1 GEEKS:2 PROGEEK:3
Splitting on Space
这里的空格不仅意味着‘,还包括换行符,标签等。
例:
# Perl program to demonstrate the
# splitting on space
#!/usr/bin/perl
use strict;
use warnings;
# string to be splitted
my $str = "ProGeek\n\nSudo\nPlacements";
# using split function
my @spl = split(' ', $str);
# Displaying result by printing
# 'GFG' either side of the
# value, so that user can see
# where it split
foreach my $i (@spl)
{
print "GFG${i}GFG\n";
}
GFGProGeekGFG GFGSudoGFG GFGPlacementsGFG
Important Points To Remember
- 由于split()函数还返回标量上下文中的值。因此,为了存储返回值,用户必须根据拆分部分的数量定义一些标量值。在下面的示例中,拆分后将有4个值,因此用户将在此处定义4个标量值并存储返回值。
例:
# Perl program to demonstrate the # splitting on string and storing # values in scalars #!/usr/bin/perl use strict; use warnings; # string which is seperated by !! sign my $str = 'GFG!Sudo!GeeksforGeeks!ProGeek'; # using split function and # storing values in scalars my ($sc1, $sc2, $sc3, $sc4) = split('!', $str); # displaying string after splitting print "$sc1\n$sc2\n$sc3\n$sc4";
输出:GFG Sudo GeeksforGeeks ProGeek
- 可能存在以下情况:用户不传递要分割的字符串,则默认情况下,split()函数将使用$_;如果用户不传递表达式,即不分割字符串,则将使用''(a空间)。
例:
# Perl program to demonstrate the # split() function and context #!/usr/bin/perl use strict; use warnings; # using foreach loop containing string values foreach ('G F G', 'Geeks for Geeks') { # using split() function my @spl = split; # displaying values to be split print "Split $_:\n"; foreach my $i (@spl) { print " $i\n"; } }
输出:Split G F G: G F G Split Geeks for Geeks: Geeks for Geeks
- 如果分隔符出现在要分割的字符串的开头,则返回值的第一个元素将为空,并将存储到数组中。在下面的示例中,我们遇到这种情况,并且正在打印结果数组的空值:
例:
# Perl program to demonstrate the # split() function with the Delimiter # at the start of the string #!/usr/bin/perl use strict; use warnings; # string containing delimiter(, ) # at the starting my $str = ', GFG, Geeks'; # using split function my @spl = split(', ', $str); # printing "Array_Element: " with each # returned value so that you can see # the empty one foreach my $i (@spl) { print "Array_Element: $i\n"; }
输出:Array_Element: Array_Element: GFG Array_Element: Geeks
- 如果要保留定界符,也只需将该定界符放在括号内即可。
例:
# Perl program to demonstrate the # split() function and keeping # the delimiter #!/usr/bin/perl use strict; use warnings; # string to be split my $str = 'Geeks1for2Geeks'; # using split function # \d+ will match one or more # integer numbers & placed # between two // and () to # keep the delimiter in result my @spl = split(/(\d+)/, $str); # displaying string after splitting foreach my $i (@spl) { print "$i\n"; }
输出:Geeks 1 for 2 Geeks
相关用法
- Perl log()用法及代码示例
- Perl exp用法及代码示例
- Perl int()用法及代码示例
- Perl abs()用法及代码示例
- Perl uc()用法及代码示例
- Perl tell()用法及代码示例
- Perl chr()用法及代码示例
- Perl hex用法及代码示例
- Perl oct()用法及代码示例
- Perl cos()用法及代码示例
- Perl each()用法及代码示例
- Perl ord()用法及代码示例
- Perl sin()用法及代码示例
- Perl quotemeta()用法及代码示例
- Perl atan2()用法及代码示例
注:本文由纯净天空筛选整理自Aks_Aggarwal大神的英文原创作品 Perl | split() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。