示例:
%hash = (2010 => 21, 2009=> 9);
$hash = {
a => {
0 => {test => 1},
1 => {test => 2},
2 => {test => 3},
3 => {test => 4},
},
};
如何打印哈希?
答案 0 :(得分:16)
是否要打印整个哈希值或特定键值对?你期望的结果是什么?如果它仅用于调试目的,您可以执行以下操作:
use Data::Dumper;
print Dumper %hash; # or \%hash to encapsulate it as a single hashref entity;
如果您不关心订购,可以使用each
功能:
while ( my($key, $value) = each %hash ) {
print "$key = $value\n";
}
如果要对其进行排序,请使用for
/ foreach
构造:
for my $key ( sort keys %hash ) {
print "$key = $hash{$key}\n";
}
或者,如果您只想要某些值,则可以使用哈希切片,例如:
print "@hash{qw{2009 2010}}\n";
等等,总是有不止一种方法可以做到这一点,虽然它有助于你知道你最先要做的是什么:)
答案 1 :(得分:2)
while( my( $key, $value ) = each( %hash ) ) {
...
}
答案 2 :(得分:2)
而不是
%hash = { 2010=> 21, 2009=> 9 }
你应该写
%hash = ( 2010=> 21, 2009=> 9 );
使用花括号,您可以获得匿名哈希的参考, 然后将其存储为%hash的第一个键。
答案 3 :(得分:2)
第二个示例访问内部单元格的语法如下:
print $hash->{"a"}{0}{"test"}
在你的例子中,这将给你1个。
如果你想迭代它,可以按如下方式进行(打印行用于说明目的):
my $hash = {"a"=>{ 0=>{"test"=>1}, 1=>{"test"=>2}, 2=>{"test"=>3}, 3=>{"test"=>4} } };
print "Direct access to item : ".$hash->{"a"}{1}{"test"}."\n";
foreach my $k1 (keys(%$hash)) {
print "keys of level 1: $k1\n";
foreach my $k2 (keys(%{$hash->{$k1}})) {
print "keys of level 2: $k2\n";
print "values: ".$hash->{$k1}{$k2}{"test"}."\n"
}
}
请注意,事情比必要的更棘手,因为外部$ hash是匿名哈希的标量引用。如果它是一个哈希值会更简单(例如,像my %hash = (1, 2); print $hash{1};
)。
( TIMTOWTDI :显然有多种方法可以做到;我认为上面的示例对我来说最简单,但效率最高;使用each
代替{ {1}}迭代会避免一个不必要的哈希查找。)
答案 4 :(得分:1)
你可以试试这个,
while(my ($key,$val)=each %HASH){
print $key," = ",$val,"\n";
while(my ($kkey,$vval)=each %{$HASH{$key}}){
print " ",$kkey," = ",$vval,"\n";
}
}
答案 5 :(得分:0)
使用keys , values
功能
@keys = keys %hash ;
@values = values %hash
答案 6 :(得分:0)
这应该有所帮助:
foreach $key (keys %hash)
{
print "key is : $key, value is : $hash{$key}\n";
}
欢呼声
答案 7 :(得分:0)
printf ("%s = %s\n", $_, $hash {$_}) foreach (keys (%hash));
答案 8 :(得分:0)
下面的函数printStruct使用递归工作,可以将数组的散列,散列数组或其任何混合打印到任何深度。您可以通过对结构的引用以及字符串中结构的名称来调用它。最后一个输入$ pre仅在递归期间用于告知初始进入递归函数。调用该函数时,请将其留空。
%hash = (2010 => 21, 2009=> 9);
printStruct(\%hash,"\%hash");
$hash = {
a => {
0 => {test => 1},
1 => {test => 2},
2 => {test => 3},
3 => {test => 4},
},
};
$hash->{b}=[1..5];
printStruct($hash,"\$hash");
my @array=[apple,banana,orange,$hash];
printStruct(\@array,"\@array");
sub printStruct {
my ($struct,$structName,$pre)=@_;
print "-----------------\n" unless (defined($pre));
if (!ref($struct)){ # $struct is a scalar.
print "$structName=$struct\n";
} elsif (ref($struct) eq "ARRAY") { # Struct is an array reference
return ("ARRAY(".scalar(@$struct).")") if (@$struct>100);
for(my$i=0;$i<@$struct;$i++) {
if (ref($struct->[$i]) eq "HASH") {
printStruct($struct->[$i],$structName."->[$i]",$pre." ");
} elsif (ref($struct->[$i]) eq "ARRAY") { # contents of struct is array ref
print "$structName->"."[$i]=()\n" if (@{$struct->[$i]}==0);
my $string = printStruct($struct->[$i],$structName."->[$i]",$pre." ");
print "$structName->"."[$i]=$string\n" if ($string);
} else { # contents of struct is a scalar, just print it.
print "$structName->"."[$i]=$struct->[$i]\n";
}
}
return();
} else { # $struct is a hash reference or a scalar
foreach (sort keys %{$struct}) {
if (ref($struct->{$_}) eq "HASH") {
printStruct($struct->{$_},$structName."->{$_}",$pre." ");
} elsif (ref($struct->{$_}) eq "ARRAY") { # contents of struct is array ref
my $string = printStruct($struct->{$_},$structName."->{$_}",$pre." ");
print "$structName->"."{$_}=$string\n" if ($string);
} else { # contents of struct is a scalar, just print it.
print "$structName->"."{$_}=$struct->{$_}\n";
}
}
return();
}
print "------------------\n" unless (defined($pre));
return();
}
结果:
-----------------
%hash->{2009}=9
%hash->{2010}=21
-----------------
$hash->{a}->{0}->{test}=1
$hash->{a}->{1}->{test}=2
$hash->{a}->{2}->{test}=3
$hash->{a}->{3}->{test}=4
$hash->{b}->[0]=1
$hash->{b}->[1]=2
$hash->{b}->[2]=3
$hash->{b}->[3]=4
$hash->{b}->[4]=5
-----------------
@array->[0]->[0]=apple
@array->[0]->[1]=banana
@array->[0]->[2]=orange
@array->[0]->[3]->{a}->{0}->{test}=1
@array->[0]->[3]->{a}->{1}->{test}=2
@array->[0]->[3]->{a}->{2}->{test}=3
@array->[0]->[3]->{a}->{3}->{test}=4
@array->[0]->[3]->{b}->[0]=1
@array->[0]->[3]->{b}->[1]=2
@array->[0]->[3]->{b}->[2]=3
@array->[0]->[3]->{b}->[3]=4
@array->[0]->[3]->{b}->[4]=5
这个函数有助于复杂结构的大量编程和调试。我希望你们觉得它很有用。