gpt4 book ai didi

perl - 是在这里我要使用的还是弹出的?

转载 作者:行者123 更新时间:2023-12-03 15:37:50 24 4
gpt4 key购买 nike

我发现了关于split的一些奇怪之处。我猜它不会解析空白内容,直到最后有东西。

我有一个以逗号分隔的字符串。但是,当末尾有重复的定界符时,它们将不会添加到数组中,直到末尾有如下内容:

use warnings;
use strict;
use Data::Dumper;

my $str = "ABC,123,,,,,,"; # just ABC and 123
#my $str = "ABC,123,,,,,,this"; # now it shows the blanks.
my @elems = split ',', $str;
print Dumper \@elems;

还有其他方法,例如正则表达式?还是我应该欺骗数组,然后对最后的内容 pop编码?

最佳答案

您可以指定一个负数LIMIT:

my $str = "ABC,123,,,,,,";
my @elems = split ',', $str, -1;

split:

If LIMIT is negative, it is treated as if it were instead arbitrarily large; as many fields as possible are produced.

If LIMIT is omitted (or, equivalently, zero), then it is usually treated as if it were instead negative but with the exception that trailing empty fields are stripped


Dumper输出:
$VAR1 = [
'ABC',
'123',
'',
'',
'',
'',
'',
''
];

关于perl - 是在这里我要使用的还是弹出的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62245783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com