Why is the list my Perl map returns just 1's?
dictionary, perl
Solution
Ok, first off, you probably meant to have `$_ =~ s/\..*$//` — note the missing `s` in your example. Also, you probably mean `map` not `grep`.
Second, that doesn't do what you want. That actually modifies `@input`! Inside `grep` (and `map`, and several other places), `$_` is actually aliased to each value. So you're actually changing the value.
Also note that pattern match does not return the matched value; it returns true (if there is a match) or false (if there isn't). That's all the 1's you're seeing.
Instead, do something like this:
my @output = map {
(my $foo = $_) =~ s/\..*$//;
$foo;
} @input ;
The first copies `$_` to `$foo`, and then modifies `$foo`. Then, it returns the modified value (stored in `$foo`). You can't use `return $foo`, because its a block, not a subroutine.
Problem
The code I wrote is as below : ``` #!/usr/bin/perl my @input = ( "a.txt" , "b.txt" , "c.txt" ) ; my @output = map { $_ =~ s/\..*$// } @input ; print @output ; ``` My intention is to let the file name without the extension stored in the array `@output`. but instead it stores the value returned by `s///` rather than the changed file name in `@output`, so the result looks like ``` 1 1 1 ``` so what is the correct way to use `map` under this situation?