Why is the list my Perl map returns just 1's?

后端 未结 8 1862
时光说笑
时光说笑 2021-01-02 07:32

The code I wrote is as below :

#!/usr/bin/perl 

my @input = ( \"a.txt\" , \"b.txt\" , \"c.txt\" ) ;
my @output = map { $_ =~ s/\\..*$// } @input ;

print @o         


        
8条回答
  •  长发绾君心
    2021-01-02 08:10

    You are missing the 's' for substitution.

    $_ =~ /\..*$//
    

    should be

    $_ =~ s/\..*$//
    

    Also you might be better off to use s/\.[^\.]*$// as your regular expression to make sure you just remove the extension even when the filename contains a '.' (dot) character.

提交回复
热议问题