C1 C2 C3 C4 C5
11 12 13 14 15
21 22 23 24 25
my $data = [
[11,12,13,14,15],
[21,22,23,24,25]
];
my $column_ids = ["C1", "C2", "C3", "C4", "C5"];
# %h - {'C1'=>'x1', 'C2'=>'x2', 'C3'=>'x3', 'C5'=>'x4', 'C5'=>'x5'}
my $results = [map { my %h; @h{ @$column_ids } = split(",", $_); \%h } @$data];
results得到一个hash数组
results[0] - {'C1'=>'11', 'C2'=>'12', 'C3'=>'13', 'C5'=>'14', 'C5'=>'15'}
results[1] - {'C1'=>'21', 'C2'=>'22', 'C3'=>'23', 'C5'=>'24', 'C5'=>'25'}
[url]http://stackoverflow.com/questions/95820/in-perl-how-do-i-create-a-hash-whose-keys-come-from-a-given-array[/url]
本文介绍了一种在Perl编程语言中将二维数组转换为Hash数组的方法,通过使用特定的列标识符作为键来映射每一行的数据。这种方法对于处理表格数据特别有用。
174

被折叠的 条评论
为什么被折叠?



