perl 用foreach 求解遍历HASH
刚自学perl一个小时
求高手指点 一个HASH如下 "小明"=>(语文=>50 数学=>60)
"小刚"=>(语文=>80 数学=>90)
将HASH 遍历出来
[解决办法]
%hash = ("小明"=>{'语文'=>50, '数学'=>60}, "小刚"=>{'语文'=>80, '数学'=>90});while (($k, $v) = each %hash) { print "$k =>\n"; while (($x, $y) = each %$v) { print "\t$x => $y\n"; }}
[解决办法]
赞同楼上
my %hash;%hash = ("小明"=>{'语文'=>50, '数学'=>60}, "小刚"=>{'语文'=>80, '数学'=>90});foreach my $key ( keys %hash ){ print "$key:\n"; my $subhash = $hash{$key}; foreach my $subkey ( keys %$subhash ) { print "\t$subkey => $$subhash{$subkey}\n"; }}