这是我的 perl 5.16 代码
while(<>) {
chomp;
@data = split /a/, $_;
print(join("b",@data),"\n");
}
如果我输入一个包含这个的文件:
paaaa
paaaaq
我明白了
p
pbbbbq
但我很期待
pbbbb
pbbbbq
为什么我对后一种行为的期望是错误的?
最佳答案
是documented除非您指定第三个非零参数,否则将删除尾随的空值。
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 (empty leading fields are always preserved)
你想要
split /a/, $_, -1;
关于perl - 当结果包含空字符串时在 perl 中拆分的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25903260/