看板 Perl 關於我們 聯絡資訊
如果用 Data::Dumper 來觀察 @a 的內容,就可以發現其實裡面只有兩項: use Data::Dumper 'Dumper'; my @a = split /,/, ',2,'; say Dumper(\@a); 輸出: $VAR1 = [ '', '2' ]; 所以其實 join(',',@a) 本身的行為是正確的。 而這個 split 的行為則有寫在文件 perldoc -f split 當中:如果尾巴的內 容是空的,就會被截掉。如果要保留空白,就在第三個參數位置上放個負數: my @a = split /,/, ',2,', -1; 文件:https://perldoc.perl.org/functions/split.html 以下引用文件內文相關的兩段: 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 (empty leading fields are always preserved); if all fields are empty, then all fields are considered to be trailing (and are thus stripped in this case). Thus, the following: print join(':', split(/,/, 'a,b,c,,,')), "\n"; produces the output a:b:c , but the following: print join(':', split(/,/, 'a,b,c,,,', -1)), "\n"; produces the output a:b:c::: . 我猜測這行為多半是某種字串處理的優化。 ---- Sent from BePTT -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.40.19.26 ※ 文章網址: https://www.ptt.cc/bbs/Perl/M.1520074845.A.03B.html
gecer: 只能推了~~ 03/04 11:14