作者Solberg (packing for trip)
看板Perl
标题[问题] 替换第一个空白为\t
时间Wed May 18 14:13:04 2016
各位版大好
小弟有段时间没碰perl,最近作一个字串处理,要将每个字
第一个空白取代为tab(\t)
ex:
"纸盒 5x10x1" -> "纸盒 5x10x1"
我写的程式如下
--------------------------------------------------------------
use strict;
use warnings;
my $filename = 'token'; //开档案token
open(my $fh, $filename)
or die "Could not open file '$filename' $!";
while (my $row = <$fh>) {
chomp $row;
$row =~ s/^\S+\s/$1.\t/; //比对程式
print "$row\n";
}
--------------------------------------------------------------
但出现例外
Use of uninitialized value $1 in concatenation (.) or string at test.pl line 11,
<$fh> line 10.
我记得第一个比对到的字串不是会被存在变数$1里吗?
怎麽会uninitialized value?
或者,这样的要求,有什麽更好的写法吗?感谢。
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 111.252.34.40
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Perl/M.1463551986.A.938.html
1F:推 cutekid: $row =~ s/^(\S+)\s+/$1\t/; 05/18 15:01
2F:→ Solberg: 1楼版友,感谢你,ok了。 05/19 15:04