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

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

    derobert shows you a correct way of mapping @input to @output.

    I would, however, recommend using File::Basename:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use File::Basename;
    
    my @input = qw( a.1.txt b.txt c.txt );
    my @output = map { scalar fileparse($_, qr/\.[^.]*/) } @input ;
    
    use Data::Dumper;
    print Dumper \@output;
    

    Output:

    C:\Temp> h
    $VAR1 = [
              'a.1',
              'b',
              'c'
            ];
    

提交回复
热议问题