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

后端 未结 8 1904
时光说笑
时光说笑 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:01

    Out of all of those answers, no one simply said that map returns the result of the last evaluated expression. Whatever you do last is the thing (or things) map returns. It just like a subroutine or do returning the result their last evaluated expression.

    Perl v5.14 adds the non-destructive substitution, which I write about in Use the /r substitution flag to work on a copy. Instead of returning the number of replacements, it returns the modified copy. Use the /r flag:

    my @output = map { s/\..*$//r } @input;
    

    Note that you don't need to use the $_ with the binding operator since that's the default topic.

提交回复
热议问题