带分隔符的Concat字符串

时间:2017-07-27 18:58:48

标签: perl

$ ls /tmp/foo/
foo10.txt  foo11.txt  foo15.txt

$ cat ./foo.pl 
use warnings;
use strict;
use Cwd;
my $dir = cwd();
chdir '/tmp/foo';
my @files = glob "foo*.txt";
my $b = "";
for (0..$#files) {
  my ($a) = $files[$_] =~ m/foo(.*)\.txt/;
  $b = $b.",".$a;
}
chdir $dir;
print "$b\n";

输出:

$ perl ./foo.pl 
,10,11,15

如何在10点之前避免使用第一个逗号?如果有比这更好的逻辑,请建议。

1 个答案:

答案 0 :(得分:1)

say join ', ', map { /foo(.*)\.txt/ } glob "foo*.txt";

首先构建列表/数组,然后使用join