function file_read(table,file_name) local get_buf="" local k=1 local key=0 local v=0 local file_num file_num=_FOPEN(file_name,"r")
while get_buf==nil do _FGETS(file_num,get_buf) for key,v in string.gfind(get_buf, "([%w]+)=([-]?[%w]+)") do table[k][tonumber(key)]=tonumber(v) end k=k+1 end _FCLOSE(file_num) end
function rnext2(t, keys) keys = keys or {} local k, v local ks, c = {t}, (#keys > 1) and #keys or 1 for i = 2, c do ks[i] = ks[i-1][keys[i-1]] end k, v = next(ks[c], keys[c]) while (k == nil) and (c > 1) do ks[c], keys[c], c = nil, nil, c-1 k, v = next(ks[c], keys[c]) end if k == nil then return nil end keys[c] = k while type(v) == 'table' do k, v = next(v, nil) keys[#keys+1] = k end return keys, v end
function envdofile(filename) local c = loadfile(filename) local e = {} local m = getmetatable(e) or {} m.__index = getfenv(c) or _G setmetatable(e, m) setfenv(c, e) return e, c() end
926 のを循環参照に対応させてみた。 function each(t) local cache = {} local function e(t) cache[t] = true local k, v = next(t) while k do if cache[v] then elseif type(v) == "table" then e(v) else coroutine.yield(v) end k, v = next(t, k) end end return coroutine.wrap(e), t end
--compare many types (over spec...) function table_lt_event(op1, op2) local type1, type2 = type(op1), type(op2) if type1 ~= type2 then --cmp type return type1 < type2 elseif type1 == "number" and type2 == "number" or type1 == "string" and type2 == "string" then return op1 < op2 --default elseif type1 == "boolean" and type2 == "boolean" then return op1 == true else return tostring(op1) < tostring(op2) --cmp address end end
--sorted keys -- Using: for i, k in pairs(table.keys(t)) do ... end function lua_table_keys(t) local keys = {} for k in pairs(t) do table.insert(keys, k) end table.sort(keys, table_lt_event) return keys end
--sorted pairs -- Using: for k, v in spairs(t) do ... end function lua_table_spairs(t) local k = {} local function e(t) for i, v in pairs(lua_table_keys(t)) do coroutine.yield(v, t[v]) end end return coroutine.wrap(e), t end
exception=setmetatable({throw=error},{__newindex= function(t,k,v)if k=="catch"then xpcall(t.try,v) elseif k=="finally"then(function(e,m,...)v(e,m, ...)if not e then error(m,0)end end)(pcall(t.try)) else rawset(t,k,v)end end})
exception.try =function() exception.throw("hogehoge") end exception.finally =function() print("finally") end