Alex Gordon
Alex Gordon

Reputation: 60811

Choosing the right data structure enum/struct/class?

I am parsing a file that looks like this:

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

i need to parse all this data and capture it in some kind of data structure.

here is what i came up with:

 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; }
        }

    }

i dont think this is the right solution because i would want to say

string somestring=LuaStructure.gt.rs1080985;

this would not be possible because i would need to first declare;

LuaStructure somestructure = new LuaStructure;

then i would need to do

LuaStructure.gt somestructuregt = new LuaStructure.gt;

I dont want to have to go through this entire process

what combination of class/struct/enum do i need to capture the data that i want?

Upvotes: 1

Views: 239

Answers (1)

M.Babcock
M.Babcock

Reputation: 18965

You aren't going to benefit from doing this in structs since the properties/types will require explicit parsing and assignment. I tend to avoid this but you may actually benefit from using RegEx or basic string manipulation to transform your input into JSON since the format is fairly close. Doing so, you could at least leverage existing parsers to handle the heavy lifting for you.

Upvotes: 1

Related Questions