简体   繁体   English

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

[英]Best way to handle complicated nested data using Perl


I am relatively new to programming. 我是编程新手。 I have to process a massive file containing nested data on married couples like below. 我必须处理一个巨大的文件,其中包含已婚夫妇的嵌套数据,如下所示。 I tried to use writing a program in Perl but setting up so many flags to go into each of the nested data beginning { and ending with } does not seem like an efficient way to process this data. 我尝试使用Perl编写程序,但是设置很多标志以放入每个以{开头并以}结尾的嵌套数据中,似乎不是一种有效的处理这些数据的方法。 I am seeking your advice on how to best handle data like below in Perl. 我正在就如何最好地处理Perl中的数据寻求您的建议。

Thanks a ton. 万分感谢。 Andy 安迪

I have some data that looks like below: 我有一些看起来像下面的数据:

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

If this is task you may do repeatably, I would consider writing simple parser for this mini-language. 如果这是您可以重复执行的任务,那么我会考虑为这种迷你语言编写简单的解析器。 Doesn't seem to be very hard even from scratch, https://metacpan.org/pod/Parse::RecDescent may be of some use (if not overkill). 即使从头开始,似乎也不是很困难, https: //metacpan.org/pod/Parse :: RecDescent可能有一定用处(即使不是过度杀伤)。

If I were in a hurry, had to handle such file once, and this file would be below 100MB, I would .... open a copy of that in emacs and make a few regexp replaces to make it correct perl by itself (or, if the file is very big, wrote short script to make those regexp replacemnets). 如果我很着急,必须处理一次该文件,并且该文件将小于100MB,我将在Emacs中打开该文件的副本,并进行一些regexp替换,以使其本身成为正确的perl(或,如果文件很大,则编写简短的脚本来进行这些正则表达式替换。 That's not far, sth like: 那不远,就像……

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),
    },

is proper perl, so changes like 是适当的perl,因此更改如下

  • swap :(anything); 交换:(任何东西); with => "(anything)", 与=>“(任何)”,
  • add , after every } 在}之后添加
  • make qw out of ("...") 从(“ ...”)中使qw

will go close... 将关闭...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Perl解析XML中嵌套属性的最佳方法是什么? - What's the best way to parse nested attributes in XML using Perl? 在 Perl 中处理异常的最佳方法是什么? - What is the best way to handle exceptions in Perl? 使用jQuery / Perl / Mechanize处理不同单引号(')和(')的最佳方法是什么? - What's the best way to handle different single quotes (’) and (') using jQuery/Perl/Mechanize? 在perl中处理项目级变量的最佳方法是什么 - What the best way to handle project level variables in perl 在Perl中处理多个传出连接的最佳方法是什么? - What's the best way to handle multiple outgoing connections in Perl? 使用Perl验证XML,解析XML并将数据存储回MySQL数据库的最佳方法是什么? - What is the best way to validate XML against XML Schema, parsing it and storing data back to MySQL Database using Perl? 在 Perl 中制作数据结构的深层副本的最佳方法是什么? - What's the best way to make a deep copy of a data structure in Perl? 使用Perl在下划线之间提取字符串的最佳方法 - Best way to extract string between underscores using Perl 使用Moose的Perl OO - 编写委派示例的最佳方法? - Perl OO using Moose - best way to code delegation example? Perl嵌套数据结构 - Perl nested data structures
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM