Using a Perl Map Local Variable

This code compiles the set using hash keys of unique stubs of the base name in the set of paths.

%stubs = map { $f=basename $_; $f =~ /^([A-Za-z]+[0-9]+)\./ ; $1=>() } @pathlist;

Why do I need links $fhere? I thought that everything would be with me:

%stubs = map { basename; /^([A-Za-z]+[0-9]+)\./; $1=>() } @pathlist;

But I do not meet. Am I not allowed to change $ _ in a map block?



For those who are wondering what the code is doing:

For each $ path (@pathlist), it gets a base name by matching the first sequence of alphabetic numbers, and then returns the first match as a key as the value of an empty list. Example:

/some/dir/foo123.adfjijoijb
/some/dir/foo123.oibhobihe
/some/dir/bar789.popjpoj

returns

foo123 => ()
bar789 => ()

After that, I use the map keys as a set of values, so I process it.

+5
source share
3 answers

basename $_. $f:

%stubs = map { basename($_) =~ /^([A-Za-z]+[0-9]+)\./; $1 => undef } @pathlist;

:() , ; , . $1 => () % .

, $1:

%stubs = map { basename($_) =~ /^([A-Za-z]+[0-9]+)\./ ? ($1 => undef) : () } @pathlist;

, - undef, :

%stubs = map { basename($_) =~ /^([A-Za-z]+[0-9]+)()\./ } @pathlist;
+13

map grep $_ . , . , , , , , - , % stubs @pathlist , .

: File:: Basename basename $_. .

#!/usr/bin/perl
use feature say;
use File::Basename;

@pathlist=("/some/dir/foo123.adfjijoijb","/some/dir/foo123.oibhobihe","/some/dir/bar789.popjpoj");
%stubs1 = map { $f=basename $_; $f =~ /^([A-Za-z]+[0-9]+)\./ ; $1=>() } @pathlist;
say join(',',keys %stubs1);
say "---";
say join(',',@pathlist);
say "---";

%stubs = map { $_=basename $_; /^([A-Za-z]+[0-9]+)\./; $1=>() } @pathlist;
say join(',',keys %stubs);
say "---";
say join(',',@pathlist);
say "---";

%stubs = map {basename; /^([A-Za-z]+[0-9]+)\./; $1=>() } @pathlist;
+6

Alternative implementation:

my %stubs =
   map { $_ => undef }
   map { basename($_) =~ /^([A-Za-z]+[0-9]+)\./ }
   @pathlist;
0
source

All Articles