---
title: "Perl - Array to Hash"
author: "xmeteor"
date: "2012-12-28T15:24:23Z"
tags: []
categories: ["编程语言"]
slug: "perl-array-to-hash"
draft: false
lastmod: "2012-12-28T15:55:54Z"
id: 1756437
url: "https://xmeteor.iteye.com/blog/1756437"
---

```java
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 - {'C1'=>'11', 'C2'=>'12', 'C3'=>'13', 'C5'=>'14', 'C5'=>'15'}
results - {'C1'=>'21', 'C2'=>'22', 'C3'=>'23', 'C5'=>'24', 'C5'=>'25'}

<http://stackoverflow.com/questions/95820/in-perl-how-do-i-create-a-hash-whose-keys-come-from-a-given-array>