高手请进 perl 子散列的问题 -- 满意加分!

WG_Wolf 2009-05-22 06:11:43
这个问题困扰小弟很久了,
比如:
my %hello = {
config => {
config1 => "went" ,
config2 => "love"
},
label => \{
label1 => "1234",
label2 => "2345"
},
cont => "zhangwetnao"
};

我如何才能遍历config子散列??小弟初学perl请高手们详细指教~~
我用each得到结果不对啊!
对了,最好能用引用访问!@_@
...全文
104 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
fibbery 2009-05-23
  • 打赏
  • 举报
回复
遍历config
foreach my $k (keys(%{$hello{config}}))
{
print("$k => $hello{config}{$k}\n");
}
WG_Wolf 2009-05-23
  • 打赏
  • 举报
回复
谢谢 两位小弟不胜感激!
fibbery 2009-05-22
  • 打赏
  • 举报
回复
my %hello = {#错误,给哈希变量赋值要使用括号“()”
config => {#正确,对key config对应的value赋予哈希引用
config1 => "went" ,
config2 => "love"
},
label => \{#此处成了哈希引用的引用
label1 => "1234",
label2 => "2345"
},
cont => "zhangwetnao"
};


正确的代码:
my %hello = (
config => {
config1 => "went" ,
config2 => "love"
},
label => {
label1 => "1234",
label2 => "2345"
},
cont => "zhangwetnao"
);
ThirstyCrow 2009-05-22
  • 打赏
  • 举报
回复
use strict; 

my %hello = (
config => {
config1 => "went" ,
config2 => "love"
},
label => {
label1 => "1234",
label2 => "2345"
},
cont => "zhangwetnao"
);

while (my ($key, $value) = each %{$hello{config}}) {
print "$key = $value\n";
}
WG_Wolf 2009-05-22
  • 打赏
  • 举报
回复
怎么没什么人气啊??自己顶
WG_Wolf 2009-05-22
  • 打赏
  • 举报
回复
我用这个怎么不行啊?

my $sref = \%hello;

while ( ( $key , $value ) = each %{$sref->{'config'}} ) {
printf "[asdfsf $key=$value]\n";
}

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧