2012-02-15 13 views
0

で読み込んでいます。私はVisual Studio 2008のC#プロジェクトでluainterfaceを参照しています。ここLuaファイルをC#

は私のコードです:

private void button1_Click(object sender, EventArgs e) 
{ 
    Lua lua = new Lua(); 

    var result = lua.DoFile(@"C:\testing\regex\test-data.lua"); 

    string bla; 

    foreach (DictionaryEntry member in lua.GetTable("test_data")) 
    { 
     Console.WriteLine("({0}) {1} = {2}", 
      member.Value.GetType().ToString(), 
      member.Key, 
      member.Value); 


    } 

} 

私は取得しています出力は次のようになります。

(LuaInterface.LuaTable) 1 = table 
(LuaInterface.LuaTable) 2 = table 
(LuaInterface.LuaTable) 3 = table 
(LuaInterface.LuaTable) 4 = table 
(LuaInterface.LuaTable) 5 = table 
(LuaInterface.LuaTable) 6 = table 
(LuaInterface.LuaTable) 7 = table 
(LuaInterface.LuaTable) 8 = table 
(LuaInterface.LuaTable) 9 = table 
(LuaInterface.LuaTable) 10 = table 
(LuaInterface.LuaTable) 11 = table 
(LuaInterface.LuaTable) 12 = table 
(LuaInterface.LuaTable) 13 = table 
(LuaInterface.LuaTable) 14 = table 
(LuaInterface.LuaTable) 15 = table 
(LuaInterface.LuaTable) 16 = table 
(LuaInterface.LuaTable) 17 = table 
(LuaInterface.LuaTable) 18 = table 
(LuaInterface.LuaTable) 19 = table 
(LuaInterface.LuaTable) 20 = table 
(LuaInterface.LuaTable) 21 = table 
(LuaInterface.LuaTable) 22 = table 
(LuaInterface.LuaTable) 23 = table 
(LuaInterface.LuaTable) 24 = table 
(LuaInterface.LuaTable) 25 = table 
(LuaInterface.LuaTable) 26 = table 
(LuaInterface.LuaTable) 27 = table 
(LuaInterface.LuaTable) 28 = table 

私のデータは次のようになります。私は値を返したい

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' }, 
    }, 
}, 
{ 
    id = '001-000517', 
    cnv = { 
    e9 = 1, 
    i6 = 1, 
    }, 
    rel = { 
    rs108320985 = '>>', 
    rs38932097 = '>>', 
    rs503350655 = '>>', 
    rs10625852 = '>>', 
    rs28371725 = '>>', 
    }, 
    result = '*17/*5', 
    gt = { 
    rs1080985 = { 'C', 'C' }, 
    rs16947 = { 'T', 'T' }, 
    rs38952097 = { 'G', 'G' }, 
    rs50350655 = { 'T', 'T' }, 
    rs50330865 = { 'G', 'G' }, 
    rs5030656 = { 'AAG', 'AAG' }, 
    rs106255852 = { 'C', 'C' }, 
    rs28371706 = { 'T', 'T' }, 
    rs594321388 = { 'G', 'G' }, 
    rs769258 = { 'G', 'G' }, 
    rs28371725 = { 'G', 'G' }, 
    }, 
}, 
{ 
    id = '001-000541', 
    cnv = { 
    e9 = 1, 
    i6 = 1, 
    }, 
    rel = { 
    rs1080985 = '>>', 
    rs3892097 = '>>', 
    rs50330655 = '>>', 
    rs10625852 = '>>', 
    rs283371725 = '<<', 
    }, 
    result = '*41/*5', 
    gt = { 
    rs1080985 = { 'C', 'C' }, 
    rs16947 = { 'T', 'T' }, 
    rs3892097 = { 'G', 'G' }, 
    rs50320655 = { 'T', 'T' }, 
    rs503320865 = { 'G', 'G' }, 
    rs50350656 = { 'AAG', 'AAG' }, 
    rs10635852 = { 'C', 'C' }, 
    rs28371706 = { 'C', 'C' }, 
    rs594221388 = { 'G', 'G' }, 
    rs7693258 = { 'G', 'G' }, 
    rs283571725 = { 'A', 'A' }, 
    }, 
}, 
{ 
    id = '002-000101', 
    cnv = { 
    e9 = 1, 
    i6 = 1, 
    }, 
    rel = { 
    rs1080985 = '>>', 
    rs389532097 = '>>', 
    rs5030655 = '>>', 
    rs106235852 = '>>', 
    rs283571725 = '<<', 
    }, 
    result = '*41/*5', 
    gt = { 
    rs1080985 = { 'C', 'C' }, 
    rs16947 = { 'T', 'T' }, 
    rs38952097 = { 'G', 'G' }, 
    rs50330655 = { 'T', 'T' }, 
    rs503220656 = { 'AAG', 'AAG' }, 
    rs1065852 = { 'C', 'C' }, 
    rs283371706 = { 'C', 'C' }, 
    rs5945321388 = { 'G', 'G' }, 
    rs769258 = { 'G', 'G' }, 

私のLUAファイル内のすべての変数:id、rel、rs108 ....

何が間違っているのですか?

これはほとんど働いている 更新:

foreach (DictionaryEntry member in lua.GetTable("test_data")) 
      { 

       foreach (var keyval in ((LuaTable)member.Value).Keys) 
       { 
        MessageBox.Show(keyval.ToString()); 

それが正しいkeyvalsを通じて起こっているのではなく、私は値が各キーであるかを確認する必要があります。どうすればいい?

答えて

1

(この記事にし、複数の編集)いくつかのより多くのを確認した後、あなたはおそらく、再帰を使用してオフに最適です:

private void ProcessTable(LuaTable t, int depth) 
     { 
      depth++; 
      Console.WriteLine(new String('=', 5 * (5 - depth))); // Creates breaks between the items 
      foreach (DictionaryEntry d in t) 
      { 
       if (d.Value.GetType() == typeof(LuaTable)) 
       { 
        ProcessTable((LuaTable)d.Value, depth); 
       } 
       else 
       { 
        Console.WriteLine(String.Format("{0}={1}", d.Key, d.Value)); 
       } 
      } 
      Console.WriteLine(new String('=',5 * (5-depth))); 
     } 

その後、あなたはこのようにそれを呼び出す:

ProcessTable(lua.GetTable("test_data"), 0); 
+0

あなたに感謝そんなに助けて。私はそれをすることができるとは思わない –

+0

これは正しく聞こえる。私は、オブジェクトがキー名なしでルートテーブルに追加されているように見えるので、最初のアイテムのキーは整数だと思います。 –

+0

'((LuaTable)member.Value).Key'などと呼ぶことはできませんか? –