Perl | split()函数

split() 是一个字符串函数 Perl 这是用来分裂,或者你可以说是把一根绳子切成更小的部分或碎片。拆分字符串有不同的标准,比如单个字符上的 正则表达式 (模式)、一组字符或未定义的值等。。这个函数最好的一点是,用户可以指定将字符串拆分为多少个部分。

null

语法:

split /Pattern/, Expression, Limit

or

split /Pattern/, Expression

or

split /Pattern/

or

Split

在上述语法中, 图案 指定了一个正则表达式,该表达式提供拆分字符串的条件。这个 表示 是要拆分的字符串。这个 限度 是一种限制,它在字符串中找到的第(n-1)个模式处停止拆分。

返回值: 此方法返回中的值 两个语境 详情如下:

在数组上下文中: 在这里,它返回在表达式中找到的字段列表。如果未指定表达式,则返回$\ux。

在标量上下文中: 在这里,它返回在表达式中找到的字段数,然后将这些字段存储在@u数组中。

不同的方式 使用split()函数的步骤如下:

  • 拆分角色
  • 无限制地在字符串中拆分
  • 在带限制的字符串中拆分
  • 在未定义的值上拆分
  • 分手 正则表达式 (模式)
  • 分道扬镳 搞砸
  • 空间分裂

拆分角色

用户可以在不同的字符上断开或拆分字符串,如 逗号(,) 反斜杠() 等。这种类型的拆分通常在您必须解析来自另一个程序或文件的数据时使用。不要使用split()来解析 CSV(逗号分隔值) 文件夹。如果数据中有逗号,请使用 文本::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

无限制地在字符串之间拆分

这也与角色上的拆分相同。这里字符串的数据由两个字符分隔 !! .

例子:

# Perl program to demonstrate the
# splitting among string without Limit
#!/usr/bin/perl
use strict;
use warnings;
# string which is separated 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" ;
}


输出:

GFG
Geeks
55
GeeksforGeeks

带限制的字符串之间的拆分

这也与角色上的拆分相同。这里字符串的数据由两个字符分隔 !! 。在这里,用户可以通过在split函数中传递第三个参数来限制字符串将拆分成的节数,该参数将为正整数值。在下面的示例中,用户传递 限制为3 因此,它将限制将字符串拆分为3,即使出现4次 !! 在绳子上。

例子:

# Perl program to demonstrate the
# splitting on string with Limit
#!/usr/bin/perl
use strict;
use warnings;
# string which is separated 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" ;
}


输出:

GFG
Geeks
55!!GeeksforGeeks

在未定义的值上拆分

如果用户试图在未定义的值上拆分,则字符串将在每个字符上拆分。

例子:

# 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" ;
}


输出:

G
e
e
k
s
f
o
r
G
e
e
k
s
 
G
F
G

运行时错误:

在/home/38ececda726bcb7e68fb7b41ee5b8d9的regexp编译中使用未初始化的值。pl第12行。

在模式或正则表达式上拆分

有时,用户可能希望拆分模式(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" ;
}


输出:

Geeks
for
Geeks

分裂成一团

用户可以将数据或字符串拆分为哈希而不是数组。基本上,散列是一个键/值对。在拆分之前,用户必须了解哈希。

例子:

# 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}" ;
}


输出:

GFG:1
GEEKS:2
PROGEEK:3

空间分裂

在这里,空间不仅仅意味着 ‘ ‘ 这个空间也包括换行符、标签等。

例子:

# Perl program to demonstrate the
# splitting on space
#!/usr/bin/perl
use strict;
use warnings;
# string to be splitted
my $str = "ProGeekSudoPlacements" ;
# 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" ;
}


输出:

GFGProGeekGFG
GFGSudoGFG
GFGPlacementsGFG

要记住的要点

  • 作为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 separated 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$sc2$sc3$sc4" ;

    
    

    输出:

    GFG
    Sudo
    GeeksforGeeks
    ProGeek
    

  • 可能存在这样一种情况:用户不传入要拆分的字符串,那么默认情况下split()函数将使用 $_ 如果用户没有传递一个表达式,即要拆分的字符串,那么它将使用 “(空间) .

    例子:

    # 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 $_:" ;
    foreach my $i ( @spl )
    {
    print " $i" ;
    }
    }

    
    

    输出:

    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" ;
    }

    
    

    输出:

    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" ;
    }

    
    

    输出:

    Geeks
    1
    for
    2
    Geeks
    

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享