使用Perl处理复杂嵌套数据的最佳方法

时间:2015-02-03 21:47:28

标签: perl nested-datalist


  我对编程比较陌生。我必须处理一个包含嵌套数据的大型文件,如下所示。我试图在Perl中编写一个程序,但设置这么多标志进入每个嵌套数据开始{和结束}似乎不是处理这些数据的有效方法。我正在寻求关于如何在Perl中如何最好地处理如下数据的建议。

非常感谢。 安迪

我有一些如下所示的数据:

city{
 area : 50 sq miles ;
 population : 3000 ;
 healthIndex : 90.5/100 ;

 marriedCouples { //this begins one married couple data
  children : 2 ;
  chronologicalData() {
   date: 02-10-1990 ;
   incomeRising("TableVals") {
    values("0 1 2 3 4 5 6 7 8 9") ;
   }
   incomeFalling("TableVals") {
    values("0 1 2 3 4 5 6 7 8 9") ;
   }
  }
  child {
   name: Nathan;
   chronologicalData() {
     DOB: 02-13-1994 ;

    incomeRising("TableVals") {
     values("0 2 2 3 4 9 6 7 8 9") ;
    }
    incomeFalling("TableVals") {
     values("0 1 2 1 1 1 6 7 8 9") ;
    }
   }
   intrinsicVal() {
    risingVals {
     values("0 0.1 0.33 0.34 0.6 0.9 0.11 0.123 0.14 0.15") ;
    }
    fallingVals {
     values("0.15 0.10 0.09 0.08 0.08 0.078 0.75 0.6 0.5 0.4") ;
    }
   }
  }
 } // this finishes one married couple data

  child {  //Note that this child is not within the married couple and is a stand-alone child. It is outside of it
   name: Cody;
   chronologicalData() {
     DOB: 02-13-1974 ;

    incomeRising("TableVals") {
     values("0 12 22 33 44 49 56 57 58 59") ;
    }
    incomeFalling("TableVals") {
     values("0 41 32 21 21 19 18 17 16 15") ;
    }
   }
   intrinsicVal() {
    risingVals {
     values("0 0.1 0.331 0.34 0.6 0.9 0.11 0.123 0.14 0.125") ;
    }
    fallingVals {
     values("0.55 0.10 0.09 0.08 0.08 0.078 0.75 0.6 0.5 0.4") ;
    }
   }
  } // End stand alone child
} // End city data

1 个答案:

答案 0 :(得分:0)

如果这是你可以重复做的任务,我会考虑为这种迷你语言编写简单的解析器。即使从头开始,似乎也不是很难,https://metacpan.org/pod/Parse::RecDescent可能有一些用处(如果不是过度杀伤)。

如果我赶时间,不得不处理这样的文件一次,而且这个文件将低于100MB,我会....在emacs中打开一份副本并进行一些regexp替换以使其正确perl by本身(或者,如果文件非常大,编写短脚本来制作那些正则表达式替换网络)。那不远,就像:

city => {
    area => "50 sq miles",
    marriedCouples => { # this begins one married couple data
       children => 2, 
       #...
       values => qw(0 0.1 0.331 0.34 0.6 0.9 0.11 0.123 0.14 0.125),
    },

是正确的perl,所以改变如

  • 交换:(任何事情); with => "(任何)",
  • 在每次}
  • 之后添加
  • 使qw脱离(" ...")

将会接近......