gpt4 book ai didi

perl - 在 Perl 中递归打印数据结构

转载 作者:行者123 更新时间:2023-12-02 20:15:24 26 4
gpt4 key购买 nike

我目前正在学习 Perl。我有 Perl 哈希,其中包含对哈希和数组的引用。散列和数组又可以包含对其他散列/数组的引用。

我编写了一个子例程来递归地解析哈希并以正确的缩进打印它们。尽管例程按预期工作,但我的导师并不相信以下代码的可读性和优雅性。

我非常感谢这里 Perl 专家对以下代码的可能优化的看法。

这是我的完整代码片段..

# Array of Arrays
$ref_to_AoA = [
[ "fred", "barney" ],
[ "george", "jane", "elroy" ],
[ "homer", "marge", "bart" ],
];


#Array of Hashes
$ref_to_AoH = [
{
husband => "barney",
wife => "betty",
son => "bamm bamm",
},
{
husband => "george",
wife => "jane",
son => "elroy",
},
];

# Hash of Hashes
$ref_to_HoH = {
flintstones => {
husband => "fred",
pal => "barney",
},
jetsons => {
husband => "george",
wife => "jane",
"his boy" => "elroy", # Key quotes needed.
},
simpsons => {
husband => "homer",
wife => "marge",
kid => "bart",
},
};

# Hash which contains references to arrays and hashes
$finalHash = {
'arrayofArrays' => $ref_to_AoA,
'arrayofHash' => $ref_to_AoH,
'hashofHash' => $ref_to_HoH,
};

$string = str($finalHash);
print "$string\n";

#------------------------------------------------------------------
sub str {
my $hash = shift;
my ($space, $newline, $delimiter) = @_;
$space = "" unless (defined $space);
$newline = "\n\n\n" unless (defined $newline);
$delimiter = "\n--------------------------------------------" unless (defined $delimiter);
my $str = "";

for (sort keys %{$hash}) {
my $value = $hash->{$_};
$str .= "$newline$space$_ == $value$delimiter";
$str .= recurseErrors($value,$space);
}
$str;
}

#------------------------------------------------------------------
sub recurseErrors {
my $str;
my ($value,$space) = @_;
my $ref = ref $value;

if ($ref eq 'ARRAY') {
my $i = 0;
my $isEmpty = 1;
my @array = @$value;
$space .= "\t";
for my $a (@array) {
if (defined $a) {
$isEmpty = 0;
$str .= "\n$space$_\[$i\] :";
$str .= recurseErrors($a,$space);
}
$i++;
}
$str .= "= { }" if ($isEmpty);

} elsif ($ref eq 'HASH') {
$space .= "\t";
for my $k (sort keys %$value) {
if ( ( ref($value->{$k}) eq 'HASH') || (ref $value->{$k} eq 'ARRAY') ) {
my $val = $value->{$k};
$str .= "\n\n$space$k == ";
$str .= "$val";
}
else {
$str .= "\n$space$k == ";
}
$str .= recurseErrors($value->{$k},$space);
}

# we have reached a scalar (leaf)
} elsif ($ref eq '') {
$str .= "$value";
}
$str
}
#------------------------------------------------------------------

输出:

arrayofArrays == ARRAY(0x9d9baf8)--------------------------------------------    arrayofArrays[0] :        arrayofArrays[0] :fred        arrayofArrays[1] :barney    arrayofArrays[1] :        arrayofArrays[0] :george        arrayofArrays[1] :jane        arrayofArrays[2] :elroy    arrayofArrays[2] :        arrayofArrays[0] :homer        arrayofArrays[1] :marge        arrayofArrays[2] :bartarrayofHash == ARRAY(0x9d9bba8)--------------------------------------------    arrayofHash[0] :        husband == barney        son == bamm bamm        wife == betty    arrayofHash[1] :        husband == george        son == elroy        wife == janehashofHash == HASH(0x9da45f8)--------------------------------------------    flintstones == HASH(0x9d9bb48)        husband == fred        pal == barney    jetsons == HASH(0x9d9bbf8)        his boy == elroy        husband == george        wife == jane    simpsons == HASH(0x9d9bc48)        husband == homer        kid == bart        wife == marge

最佳答案

  1. 始终使用使用严格
  2. 要成为一个好 child ,也要使用使用警告
  3. 子例程使用的名称应该清楚地表明子例程的作用。 “recurseErrors”有点违反了这一原则。是的,它确实会递归。但什么错误呢?
  4. 在每个子例程的第一行,您应该声明并初始化所有参数。 recurseErrors 首先声明 $str,然后声明其参数。
  5. 不要像在 str() 中那样混合使用 shift 和 = @_
  6. 您可能会考虑将现在称为 recurseErrors 的内容分解为处理数组和哈希的专用例程。
  7. 无需像第 99 行和第 109 行那样引用变量。

除此之外,我认为你的教练那天过得很糟糕。

关于perl - 在 Perl 中递归打印数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1038271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com