用c#读取Lua文件

发布于 2025-01-06 03:26:07 字数 6592 浏览 0 评论 0原文

我已经在我的 Visual Studio 2008 C# 项目中成功引用了 luainterface。

这是我的代码:

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

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

i have succesfully referenced luainterface in my visual studio 2008 c# project.

here is my code:

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


    }

}

the output that i am getting is:

(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

my data 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' },
  },
},
{
  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' },

i want to return values for every variable in my lua file: id, rel, rs108....

what am i doing wrong?

update
this is almost working:

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

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

it's going through the correct keyvals, but not i need to see what the values are in each key. how do i do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

糖果控 2025-01-13 03:26:07

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

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

After reviewing some more (and multiple edits to this post), you are probably best off using recursion:

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

Then you call it like this:

ProcessTable(lua.GetTable("test_data"), 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文