【问题标题】:Choosing the right data structure enum/struct/class?选择正确的数据结构枚举/结构/类?
【发布时间】:2012-02-16 03:46:43
【问题描述】:

我正在解析一个如下所示的文件:

test_data = {
{
  id = '001-000505',
  cnv = {
    e9 = 0,
    i6 = 0,
  },
  rel = {
    rs10850985 = '-',
    rs38932097 = '-',
    rs5030655 = '-',
    rs10655852 = '-',
    rs28371725 = '-',
  },
  result = '*5/*5',
  gt = {
    rs31080985 = { '-', '-' },
    rs16947 = { '-', '-' },
    rs3892097 = { '-', '-' },
    rs503350655 = { '-', '-' },
    rs50530865 = { '-', '-' },
    rs5030656 = { '-', '-' },
    rs106371706 = { '-', '-' },
    rs59421388 = { '-', '-' },
    rs7693258 = { '-', '-' },
    rs28371725 = { '-', '-' },
  },
},
{
  id = '004-AATTGG',
  cnv = {
    e9 = 1,
    i6 = 1,
  },
  rel = {
    rs1080985 = '>>',
    rs3892097 = '>>',
    rs505306d55 = '>>',
    rs1065852 = '>>',
    rs2837d1725 = '>>',
  },
  result = '*1/*5',
  gt = {
    rs10830985 = { 'C', 'C' },
    rs164947 = { 'C', 'C' },
    rs3892097 = { 'G', 'G' },
    rs5030e655 = { 'T', 'T' },
    rs5030865 = { 'G', 'G' },
    rs5030656 = { 'AAG', 'AAG' },
    rs1065852 = { 'C', 'C' },
    rs28371706 = { 'C', 'C' },
    rs59421388 = { 'G', 'G' },
    rs769258 = { 'G', 'G' },
    rs28371725 = { 'G', 'G' },
  },
},
{
  id = '003-0300101',
  cnv = {
    e9 = 1,
    i6 = 1,
  },
  rel = {
    rs1080985 = '>>',
    rs3892097 = '>>',
    rs50530655 = '>>',
    rs10365852 = '>>',
    rs283271725 = '<<',
  },
  result = '*41/*5',
  gt = {
    rs1080985 = { 'C', 'C' },
    rs16947 = { 'T', 'T' },
    rs3892097 = { 'G', 'G' },
    rs5030655 = { 'T', 'T' },
    rs5030865 = { 'G', 'G' },
    rs5030656 = { 'AAG', 'AAG' },
    rs1065852 = { 'C', 'C' },
    rs28371706 = { 'C', 'C' },
    rs593421388 = { 'G', 'G' },
    rs7659258 = { 'G', 'G' },
    rs28371725 = { 'A', 'A' },
  },
},
{
  id = '007-CCAA',
  cnv = {
    e9 = 1,
    i6 = 1,
  },
  rel = {
    rs1080985 = '>>',
    rs38922097 = '>>',
    rs50350655 = '>>',
    rs1065852 = '>>',
    rs283371725 = '<<',
  },
  result = '*41/*5',
  gt = {
    rs1080985 = { 'C', 'C' },
    rs16947 = { 'T', 'T' },
    rs3892097 = { 'G', 'G' },
    rs50350655 = { 'T', 'T' },
    rs50350865 = { 'G', 'G' },
    rs5030656 = { 'AAG', 'AAG' },
    rs106235852 = { 'C', 'C' },
    rs28371706 = { 'C', 'C' },
    rs59421388 = { 'G', 'G' },
    rs769258 = { 'G', 'G' },
    rs28371725 = { 'A', 'A' },
  },
},
{
  id = '001-000105',
  cnv = {
    e9 = 1,
    i6 = 1,
  },
  rel = {
    rs1080985 = '>>',
    rs38392097 = '>>',
    rs5030655 = '>>',
    rs10565852 = '>>',
    rs283371725 = '>>',
  },
  result = '*1/*5',
  gt = {
    rs10820985 = { 'C', 'C' },
    rs16947 = { 'C', 'C' },
    rs32892097 = { 'G', 'G' },
    rs53030655 = { 'T', 'T' },
    rs50303865 = { 'G', 'G' },
    rs50530656 = { 'AAG', 'AAG' },
    rs1065852 = { 'C', 'C' },
    rs283751706 = { 'C', 'C' },
    rs59421388 = { 'G', 'G' },
    rs769258 = { 'G', 'G' },
    rs28371725 = { 'G', 'G' },
  },
},

我需要解析所有这些数据并将其捕获到某种数据结构中。

这是我想出的:

 struct LuaStructure
    {
        public string id { get; set; }

        public struct cnv
        {
            public string e9 { get; set; }
            public string x9 { get; set; }
            public string i6 { get; set; }
        }
        public struct rel
        {
            public string rs1080985 { get; set; }
            public string rs3892097 { get; set; }
            public string rs5030655 { get; set; }
            public string rs1065852 { get; set; }
            public string rs28371725 { get; set; }
        }

        public string result { get; set; }

        public struct gt
        {
            public string rs1080985 { get; set; }
            public string rs16947 { get; set; }
            public string rs3892097 { get; set; }
            public string rs5030655 { get; set; }
            public string rs5030865 { get; set; }
            public string rs5030656 { get; set; }
            public string rs1065852 { get; set; }
            public string rs28371706 { get; set; }
            public string rs59421388 { get; set; }
            public string rs769258 { get; set; }
            public string rs28371725 { get; set; }
        }

    }

我不认为这是正确的解决方案,因为我想说

string somestring=LuaStructure.gt.rs1080985;

这是不可能的,因为我需要先声明;

LuaStructure somestructure = new LuaStructure;

那我就需要做

LuaStructure.gt somestructuregt = new LuaStructure.gt;

我不想经历整个过程

我需要什么样的类/结构/枚举组合来捕获我想要的数据?

【问题讨论】:

  • 那个结构太糟糕了。祝你好运... 您是否考虑过在对象创建过程中使用属性初始化器?
  • @Yuck 你说得对,太糟糕了,你能推荐一些更有用的东西吗:)
  • @yuck 你能告诉我你的意思吗?我不明白如何为嵌套数据做初始化程序
  • new SomeObject { PropertyA = "", PropertyB = 1234, NestedObject = new AnotherObject { InnerValue = 123 } } 这不是一个真正的答案,所以我很抱歉将它发布在 cmets 中。
  • @Yuck 对象应该是一个类吗?你能告诉我你在答案中的意思吗?非常感谢

标签: c# .net data-structures


【解决方案1】:

structs 中执行此操作不会使您受益,因为属性/类型将需要显式解析和分配。我倾向于避免这种情况,但您实际上可能会受益于使用 RegEx 或基本字符串操作将您的输入转换为 JSON,因为格式相当接近。这样做,您至少可以利用现有的解析器为您处理繁重的工作。

【讨论】:

  • 非常感谢您的回答。你能告诉我如何用正则表达式做到这一点吗?
  • 这似乎稍微超出了您的问题范围(因此您需要在尝试自己弄清楚之后再问一个问题)。此外,您不希望我对 RegEx 的建议(我知道的只是一点点,足以让跟随我的人一团糟)。看看 JSON 规范。格式接近匹配,简单的字符串操作可能会解决问题。
  • 谢谢。你能告诉我这是怎么匹配的吗?它似乎不适合我
  • 我说这是一个接近匹配。将= 替换为: 并将单引号替换为双引号,它看起来很像一系列(集合的)JSON 集合。
  • 我唯一需要做的就是替换分号和引号?然后它会完全是 json?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-07
相关资源
最近更新 更多