1
$ 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";

Output:

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

How do I avoid first comma, just before 10? also please suggest if there is a better logic than this.

rodee
  • 3,233
  • 5
  • 34
  • 70
  • 3
    Use [`join`](https://perldoc.perl.org/functions/join.html) on a list, and don't use `$a` and `$b` as variable names---those are special and should only be used with `sort`. – Matt Jacob Jul 27 '17 at 19:03
  • Possible duplicate of [Easy way to print Perl array? (with a little formatting)](https://stackoverflow.com/questions/5741101/easy-way-to-print-perl-array-with-a-little-formatting) – Matt Jacob Jul 27 '17 at 19:10

1 Answers1

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

Build the list/array first then use join.

zdim
  • 64,580
  • 5
  • 52
  • 81
  • Needn't be in one statement of course. Can build an array and do other needed things (and checks) in that loop, then print `join`ed array. – zdim Jul 27 '17 at 20:29