簡體   English   中英

如何在Perl中打印哈希結構?

[英]How do I print a hash structure in Perl?

例子:

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

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

如何打印哈希?

是否要打印整個哈希值或特定鍵值對? 你期望的結果是什么? 如果僅用於調試目的,您可以執行以下操作:

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";

總是有不止一種方法可以做到這一點,雖然它有助於知道你要先做什么油炸:)

  while( my( $key, $value ) = each( %hash ) ) {
         ...
  }

代替

%hash = { 2010=> 21, 2009=> 9 }

你應該寫

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

使用花括號,您可以獲得匿名哈希的參考,然后將其存儲為%哈希的第一個鍵。

訪問第二個示例的內部單元格的語法如下:

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而不是用於迭代的keys將避免一個不必要的哈希查找)。

你可以試試這個,

while(my ($key,$val)=each %HASH){
print $key," = ",$val,"\n";
while(my ($kkey,$vval)=each %{$HASH{$key}}){
print "   ",$kkey," = ",$vval,"\n";
}
}

使用keys , values函數

@keys = keys %hash ; 

@values = values %hash 

這應該有所幫助:

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

干杯

printf ("%s = %s\n", $_, $hash {$_}) foreach (keys (%hash));

下面的函數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

該函數有助於復雜結構的大量編程和調試。 我希望你們發現它有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM