Extracting parts of filenames from an array 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.m4

print join(' ',@outputs) . "\n";
# config.a2 config.mk config.php

exit;
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 $&quot;, see http://perldoc.perl.org/perlvar.html

$LIST_SEPARATOR
$&quot;
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 &quot;The array is: @array\n&quot;;

is equivalent to this:
   print &quot;The array is: &quot; . join($&quot;, @array) . &quot;\n&quot;;

Mnemonic: works in double-quoted context.

Your code becomes:
# Use ALLWAYS these two lines in every script.
use strict;
use warnings;

my @configs = glob(&quot;m4/config.*.m4&quot;);
my @outputs = map{ m~m4/(config\..*)\.m4$~ } @configs;
print &quot;@configs\n&quot;;
print &quot;@outputs\n&quot;;

Output:
m4/config.a2.m4 m4/config.mk.m4 m4/config.php.m4
config.a2 config.mk config.php