所以我有这个:
@keys = qw/foo bar baz/
@values = ( [1,2,3], [4,5,6], ... )
我想将其转换为:
{ foo => 1, bar => 2, baz => 3 }, { foo => 4, bar => 5, baz => 6 }, ...
这是一个很好,简洁,惯用的方法吗?
答案 0 :(得分:6)
您需要哈希数组,因此您可以使用map
转换@values
数组,
my @keys = qw/foo bar baz/;
my @values = ( [1,2,3], [4,5,6] );
my @AoH = map { my %h; @h{@keys} = @$_; \%h } @values;
答案 1 :(得分:2)
use List::MoreUtils qw( zip );
my @keys = qw( foo bar baz );
my @values = ( [1,2,3], [4,5,6] );
my @hashes = map +{ zip @keys, @$_ }, @values;
答案 2 :(得分:1)
use List::MoreUtils qw( zip );
my @keys = qw( foo bar baz );
my @values = ( [1,2,3], [4,5,6] );
my @hashes = map { { zip @keys, @$_ } } @values;