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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。