用c#读取Lua文件

本文关键字:文件 Lua 读取 | 更新日期: 2023-09-27 18:24:57

我在visualstudio2008c#项目中成功地引用了luinterface。

这是我的代码:

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());

它正在遍历正确的键值,但我不需要查看每个键值中的值。我该怎么做?

用c#读取Lua文件

在回顾了更多内容(以及对本文的多次编辑)之后,您可能最好使用递归:

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