为什么,当我做map{printf "%4s\n",File::Spec->rel2abs($_)}glob '*';或print map{sprintf "%4s\n",File::Spec->rel2abs($_)}glob '*';4个空格字符输出到字符串的左边?...
/var/www/html/Physics/Electronics/file1.txt
/var/www/html/Physics/Electronics/file2.txt
/var/www/html/Physics/Electronics/file3.txt
/var/www/html/Physics/Electronics/file4.txt
Run Code Online (Sandbox Code Playgroud)
似乎我必须做map{print "\x{20}\x{20}\x{20}\x{20}",File::Spec->rel2abs($_),"\n"}glob '*';的是输出字符串左边的4个空格字符...
/var/www/html/Physics/Electronics/file1.txt
/var/www/html/Physics/Electronics/file2.txt
/var/www/html/Physics/Electronics/file3.txt
/var/www/html/Physics/Electronics/file4.txt
Run Code Online (Sandbox Code Playgroud)
%4s(或%-4s)并不意味着"为此字符串添加4个空格".这意味着"输出此字符串的最小字段宽度为4",即短于4个字符的字符串将用空格填充.另见perldoc -f sprintf.
如果你想在开头添加4个空格,那就行了
for my $file (glob '*') {
print " ", File::Spec->rel2abs($file), "\n";
}
Run Code Online (Sandbox Code Playgroud)
要么
for my $file (glob '*') {
print " " x 4, File::Spec->rel2abs($file), "\n";
}
Run Code Online (Sandbox Code Playgroud)
我在for这里使用是因为它比map在void上下文中更清晰,更有效(你没有收集结果列表,你只想输出一些东西).另一方面,如果您只想转换列表,那么
my @indented = map { " " x 4 . File::Spec->rel2abs($_) } glob '*';
Run Code Online (Sandbox Code Playgroud)
会工作.
| 归档时间: |
|
| 查看次数: |
40 次 |
| 最近记录: |