如何从Perl中的数组引用中优雅地创建哈希?

Dr.*_*ust 2 arrays perl hash reference

我正在寻找一种更优雅的方式来创建一个包含我从配置文件中读取的列表的哈希.这是我的代码:

read_config($config_file => my %config);

my $extension_list_reference = $config{extensions}{ext};

my @ext;

# Store each item of the list into an array

for my $i ( 0 .. (@$extension_list_reference - 1) ) {
    $ext[$i] = $extension_list_reference->[$i];
}

# Create hash with the array elements as the keys

foreach my $entry (@ext) {
    $extensions{$entry} = "include";
 }   
Run Code Online (Sandbox Code Playgroud)

谢谢.

小智 13

my %hash = map { $_ => 'include' } @list;


Nei*_*eil 6

尝试使用地图:http: //perldoc.perl.org/functions/map.html

以下是您的新代码应如下所示:

my %extensions = map { $_ => "include" } @{ $config{extensions}{ext} };
Run Code Online (Sandbox Code Playgroud)