如何从散列哈希中提取密钥名称? [英] How to extract key name from a hash of hash?

查看:140
本文介绍了如何从散列哈希中提取密钥名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 %HoH =(
flintstones => {
丈夫=>fred,
pal =>barney,
},
jetsons => {
丈夫=>george,
$ wife =>jane,
his boy =>elroy,
},
simpsons => {
husband =>homer,
wife =>marge,
kid =>bart,
},

);

如何迭代每个内部哈希(比如flintstones),并提取关键字名称(丈夫,朋友)和相应的每个这样的迭代vales?

解决方案

 为我的$ k(键%{$ HoH {flintstones}}){

my $ v = $ HoH {flintstones} {$ k};
printkey is $ k; value is $ v \\\
;
}

另一种方法是使用 each
pre $ ..}


I have following hash of hash :

%HoH = (
flintstones => {
    husband   => "fred",
    pal       => "barney",
},
jetsons => {
    husband   => "george",
    wife      => "jane",
    his boy => "elroy", 
},
simpsons => {
    husband   => "homer",
    wife      => "marge",
    kid       => "bart",
},

);

How to iterate over each inner hash (say flintstones) and also extract the key names (husband, pal) and corresponding vales for each such iteration?

解决方案

for my $k (keys %{ $HoH{flintstones} }) {

  my $v = $HoH{flintstones}{$k};
  print "key is $k; value is $v\n";
}

another way would be using each

while( my($k, $v) = each %{ $HoH{flintstones} }) { ... }

这篇关于如何从散列哈希中提取密钥名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆