How to print a hash structure in Perl?

Examples:

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

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

How to print a hash?

+4
source share
9 answers

Do you want to print whole pairs of hashes or specific keys, values? And what is your desired result? IF this is for debugging purposes only, you can do something like:

use Data::Dumper;
print Dumper %hash; # or \%hash to encapsulate it as a single hashref entity;

You can use the function eachif you do not care about the order:

while ( my($key, $value) = each %hash ) {
    print "$key = $value\n";
}

Or the for/ construct foreachif you want to sort it:

for my $key ( sort keys %hash ) {
    print "$key = $hash{$key}\n";
}

Or, if you want only certain values, you can use a hash fragment, for example:

print "@hash{qw{2009 2010}}\n";

etc. etc. There are always several ways to do this, although it helps to find out what you are frying to do first :)

+16
source
  while( my( $key, $value ) = each( %hash ) ) {
         ...
  }
+2
source

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

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

, % hash.

+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"
     }
}

, , , $ . , (.. my %hash = (1, 2); print $hash{1};).

( TIMTOWTDI: , , , , , each keys ).

+2

,

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

keys , values

@keys = keys %hash ; 

@values = values %hash 
0

:

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

0
printf ("%s = %s\n", $_, $hash {$_}) foreach (keys (%hash));
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

. , , , .

0

All Articles