使用
$hash{$key1}{$key2} = $value;
遍历(注意加粗与加下划线的语句)
foreach my $key1 (keys %hash)
{
my $hash2 = $hash{$key1};
foreach my $key2 (sort{$hash2->{$b}<=>$hash2->{a}} keys %$hash2)
{
print $key1.”\t”.$key2.”\t”.$hash2->{$key2}.”\n”;
}
}
对比单维哈希
foreach my $key (sort{$hash{$b}<=>$hash{$a}} keys %hash)
{
print $key.”\t”.$hash{$key}.”\n”;
}
实际中的应用:
比如一个二维Hash:
04 | 'lu_contract_id' => ' ', |
05 | 'lu_name' => '上海XXXX科技有限公司', |
06 | 'lu_loginid' => '上海XXXX' |
11 | 'lu_contract_id' => ' ', |
12 | 'lu_name' => '北京XXXX广告' |
13 | 'lu_loginid' => '北京XXXX' |
现在想把里面所有的lu_id组成一个列表,程序如下:
1 | foreach my $key (sort keys %hash) { |
2 | my $value = $hash{$key}; |
3 | push @list,$value->{"lu_id"}; |
阅读(3713) | 评论(0) | 转发(0) |