How do I print a hash structure in Perl?

2019-05-06 06:12发布

Examples:

%hash = (2010 => 21, 2009=> 9);

$hash = {
    a => {
        0 => {test => 1},
        1 => {test => 2},
        2 => {test => 3},
        3 => {test => 4},
    },
};

How do I print the hash?

标签: perl hash
9条回答
beautiful°
2楼-- · 2019-05-06 06:42

you can try with this,

while(my ($key,$val)=each %HASH){
print $key," = ",$val,"\n";
while(my ($kkey,$vval)=each %{$HASH{$key}}){
print "   ",$kkey," = ",$vval,"\n";
}
}
查看更多
不美不萌又怎样
3楼-- · 2019-05-06 06:43

use keys , values function

@keys = keys %hash ; 

@values = values %hash 
查看更多
等我变得足够好
4楼-- · 2019-05-06 06:43

This should help:

foreach $key (keys %hash)
{
  print "key is : $key, value is : $hash{$key}\n";
}

cheers

查看更多
登录 后发表回答