选择正确的数据结构枚举/结构/类?
我正在解析一个如下所示的文件:
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;
我不想经历整个过程,
我需要什么类/结构/枚举的组合来捕获我想要的数据?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不会从在 struct 中执行此操作中受益,因为属性/类型将需要显式解析和赋值。我倾向于避免这种情况,但实际上您可能会受益于使用 RegEx 或基本字符串操作将输入转换为 JSON,因为格式相当接近。这样做,您至少可以利用现有的解析器来为您处理繁重的工作。
You aren't going to benefit from doing this in
struct
s 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.