代码大全实践——Perl伪代码

前端之家收集整理的这篇文章主要介绍了代码大全实践——Perl伪代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
// -- 代码大全2 实践 --
1.根据伪代码写实际代码
2.循环的控制结构,可以先写内层循环,再在外面套接一层循环

场景:Perl 的 Hash of Hash
目的:打印 Hash of Hash

定义HoH表

%HoH = (
  flintstones => {
  husband => "fred",pal => "barney",},jetsons => {
  husband => "george",wife => "jane","his boy" => "elroy",# Key quotes needed.
 },simpsons => {
  husband => "homer",wife => "marge",kid => "bart",);

代码
foreach the collections
   get the family name
   for the family
       get the family roles
       then print the member(key/values)

编写内层代码

#implement the inner loop
for $roles (keys % {$HoH{$family}}) {
  print "\t $roles => $HoH{$family}{$roles},\n";
 }

套上外层代码

for $family (keys %HoH) {
 print "$family=> {\n";
 for $role (keys % {$HoH{$family}}) {
  print "\t $role => $HoH{$family}{$role},\n";
 }
 print "},\n";
}

例子虽然简单,但是展示了代码大全的建议,伪代码的编写,编程格式以及在编码碰到问题时,如何降低思维复杂度等思路。

猜你在找的Perl相关文章