I am very new to perl. So far I love it, my new favorite interpreted language. I am quickly learning that perl has MANY 'tricks' to it. The following code extracts file names out of one array and into another using a simple regex. Is there a shorter way of doing this?It feels like there's a way to remove the for loop.my @configs = glob(m4/config.*.m4);my @outputs;foreach(@configs){    $_ =~ m/m4\/(config\..*)\.m4/;    push @outputs, $1;}print join(' ',@configs) . \n;# m4/config.a2.m4 m4/config.mk.m4 m4/config.php.m4print join(' ',@outputs) . \n;# config.a2 config.mk config.phpexit;
Extracting parts of filenames from an array
beginner;regex;file system;perl
As said before, use map instead of a foreach loop and change the regex delimiter.If you put the array in double quote when print, a space is inserted between each element of the array in fact it is the content of the special variable $, see http://perldoc.perl.org/perlvar.html$LIST_SEPARATOR  $When an array or an array slice is interpolated into a double-quoted string or a similar context such as /.../ , its elements are separated by this value. Default is a space. For example, this:   print The array is: @array\n;is equivalent to this:   print The array is:  . join($, @array) . \n;Mnemonic: works in double-quoted context.Your code becomes:# Use ALLWAYS these two lines in every script.use strict;use warnings;my @configs = glob(m4/config.*.m4);my @outputs = map{ m~m4/(config\..*)\.m4$~ } @configs;print @configs\n;print @outputs\n;Output:m4/config.a2.m4 m4/config.mk.m4 m4/config.php.m4config.a2 config.mk config.php