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)){
print "$structName=$struct\n";
} elsif (ref($struct) eq "ARRAY") {
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") {
print "$structName->"."[$i]=()\n" if (@{$struct->[$i]}==0);
my $string = printStruct($struct->[$i],$structName."->[$i]",$pre." ");
print "$structName->"."[$i]=$string\n" if ($string);
} else {
print "$structName->"."[$i]=$struct->[$i]\n";
}
}
return();
} else {
foreach (sort keys %{$struct}) {
if (ref($struct->{$_}) eq "HASH") {
printStruct($struct->{$_},$structName."->{$_}",$pre." ");
} elsif (ref($struct->{$_}) eq "ARRAY") {
my $string = printStruct($struct->{$_},$structName."->{$_}",$pre." ");
print "$structName->"."{$_}=$string\n" if ($string);
} else {
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
. , , , .