0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cscの作法 その649

0
Posted at

概要

cscの作法、調べてみた。
Neo.IronLua、見つけたので、やってみた。
Luaのarrayを取り出して、ソートして返す。

サンプルコード


using System;
using Neo.IronLua;

public class LuaProcessor {
	public object SortLuaArray(object luaArray) {
		LuaTable table = luaArray as LuaTable;
		if (table != null)
		{
			try
			{
				int n = table.Values.Count;
				Console.WriteLine("[C#] ソート前のデータを処理中... 要素数: {0}", n);
				for (int i = 0; i < n - 1; i++)
				{
					for (int j = 1; j <= n - 1 - i; j++)
					{
						object val1 = table[j];
						object val2 = table[j + 1];
						if (val1 != null && val2 != null)
						{
							if (Convert.ToDouble(val1) > Convert.ToDouble(val2))
							{
								table[j] = val2;
								table[j + 1] = val1;
							}
						}
					}
				}
				Console.WriteLine("[C#] バブルソートが完了しました。");
				return table;
			}
			catch (Exception ex)
			{
				Console.WriteLine("[C#] ソート実行エラー: " + ex.Message);
			}
		}
		return null;
	}
}

public static class Program {
	public static void Main() {
		using (Lua lua = new Lua())
		{
			LuaGlobal env = lua.CreateEnvironment();
			LuaProcessor processor = new LuaProcessor();
			env["processor"] = processor;
			string luaCode = "local myData = { 52, 12, 89, 5, 23 }" + Environment.NewLine +
							 "print('[Lua] ソート前:', myData[1], myData[2], myData[3], myData[4], myData[5])" + Environment.NewLine +
							 "" + Environment.NewLine +
							 "local sortedData = processor:SortLuaArray(myData)" + Environment.NewLine +
							 "" + Environment.NewLine +
							 "print('[Lua] ソート後:', sortedData[1], sortedData[2], sortedData[3], sortedData[4], sortedData[5])";
			env.DoChunk(luaCode, "script.lua");
		}
		Console.WriteLine("続行するには Enter キーを押してください...");
		Console.ReadLine();
	}
}




実行結果

>neo5
[Lua] ソート前: 52 12 89 5 23
[C#] ソート前のデータを処理中... 要素数: 5
[C#] バブルソートが完了しました。
[Lua] ソート後: 5 12 23 52 89
続行するには Enter キーを押してください...

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?