lua 是否支持某种方式的 c 风格结构?
我想从 c 转换类似以下字体位图的内容:
struct charmatrix { char c;  int line[5]; };
static charmatrix font[] =
{
  "1", {0x2, 0x6, 0x2, 0x2, 0x2},
  "2", {0xe, 0x1, 0x6, 0x8, 0xf},
  "a" , {0x0, 0x5, 0xb, 0xb, 0x5}
}
font[letter].line[x]
[编辑] 添加了更多数据以显示非数字索引
我正在尝试像 Python 一样向字符串添加索引。这有效:
getmetatable('').__index = function(str, i) return string.sub(str, i, i) end
str1 = 'hello'    
print(str1[1])
这不会:
getmetatable('').__index = function(str, i) return str:sub(i, i) end
给出以下错误:
lua: test.lua:1: C stack overflow
stack traceback:
    test.lua:1: in function '__index'
    test.lua:1: in function '__index'
    ...
    test.lua:1: in function '__index'
    test.lua:4: in main chunk
    [C]: in ?
是否发生某种循环?为什么?
我想t使用以下格式创建一个表。
t[uniqueID] = order
该uniqueID会是唯一的,但order可以是相同或不同的各一次。
然后我想按升序对表格进行排序,以便我可以相应地打印出来uniqueID。
我的代码:
t = {}
function compare(a, b)
    return a[2] < b[2]
end
function printid()
    for k, v in pairs(t) do
        print(k)
    end
end
function main()
    t[5] = 47
    t[6] = 45
    t[7] = 49
    table.sort(t, compare)
    printid()
end
我得到的结果:
5
6
7
我期望的结果:
6
5
7
我怎样才能得到我想要的结果?
我有,我已经花了几个小时,试图找出问题,但由于Lua是还比较新的给我,我想不通为什么它不工作.
这就是我想要做的.如果我第一次敲击一个项目,它将不会出现在spikeRates表中,所以我需要添加它.如果我之前已经刺穿了这个项目,但从未使用过我喜欢的项目,那么我需要在spikeRates [itemSpiked]表中反映出来.我写的代码如下,但无法填充表格.
spikeRates={}
itemSpiked = "leather"
mySpike = "iron"
if not spikeRates[itemSpiked] then
   spikeRates[itemSpiked]={}
   spikeRates[itemSpiked][mySpike]={}
   print("This is your first time spiking "..itemSpiked.."!") 
 elseif not spikeRates[itemSpiked][mySpike] then 
   spikeRates[itemSpiked][mySpike]={Failure=0,Success=0} 
   print("This is your first time spiking "..itemSpiked.." with "..mySpike.."!")
end
for i,v in pairs(spikeRates) do 
  print(i .. ": " .. v) 
end
当它运行时,我得到一个错误,试图连接数据库打印的零值(v).一切看起来都是正确的,或者我认为.我错过了一些完全明显的东西吗 谢谢!
我正在尝试将数据库的数据类别的数据值加载到表中以供进一步处理.每个数据类别都应该有自己的表.遗憾的是,数据类别的数量不一致,因DB而异,因此我考虑根据当前数据类别的数量自动创建表:
--categories is a table containing the names of all data categories
for a = 1, #categories, 1 do 
temptable..a = {}; 
end
这当然不起作用,因为Lua尝试将表分配给变量而不是其值.这种方式也不可能进行变量连接.有没有办法让Lua自动创建表格?
我需要替换lua中的键的值,例如考虑一个表
t = {"book", "ball", "bank"}
在这里我需要更改"box"而不是"ball"的值.怎么做 ?
以前我试过找到一个关键值并改变,但它没有用!!!
for key, value in pairs(t) do
  if key == 2 then
    value = "box"
  end
end
但它没有用..如果有人知道替代方式,请给我建议?
背景:
我正在努力教自己Lua,我很难理解为什么一个表在其中包含数据时被认为是nil.任何人都可以为我分解为什么我从下面的代码片段中收到此错误消息?这是我的第一个程序之一,我真的需要在继续我的真实项目之前先了解这几个概念.谢谢!
错误信息:
C:\Users\<user>\Desktop>lua luaCrap.lua
lua: luaCrap.lua:7: attempt to call global 'entry' (a nil value)
stack traceback:
        luaCrap.lua:7: in main chunk
        [C]: ?
码:
--this creates the function to print
function fwrite (fmt, ...)
  return io.write(string.format(fmt, unpack(arg)))
end
--this is my table of strings to print
entry{
    title = "test",
    org = "org",
    url = "http://www.google.com/",
    contact = "someone",
    description = [[
                    test1
                    test2
                    test3]]
}
--this is to print the tables first value  
fwrite(entry[1])
--failed loop attempt to print …以下示例应该创建一个表,可以在数字和字符串之间转换,然后再返回,但无法运行.
是因为我在字典类型中使用数字键吗?或者是因为lua从1开始表索引?
有没有更好的方法来实现这一目标?
dyeColor = {
    0="black"    ,  black     = 0,
    1="red"      ,  red       = 1, 
    2="green"    ,  green     = 2,
    3="brown"    ,  brown     = 3,
    4="blue"     ,  blue      = 4,
    5="purple"   ,  purple    = 5,
    6="cyan"     ,  cyan      = 6,
    7="lightGray",  lightGray = 7,
    8="gray"     ,  gray      = 8,
    9="pink"     ,  pink      = 9,
    10="lime"     ,  lime      =10,
    11="yellow"   ,  yellow    =11,
    12="lightBlue",  lightBlue =12,
    13="magenta"  ,  magenta   =13,
    14="orange"   ,  orange    =14,
    15="white"    ,  white     =15}
使用这个在线翻译(http://repl.it/languages/Lua …
我试图比较两个字符串,其中第一个字符串是数组的单个元素,第二个数组只是一个常规字符串.出于某种原因,这种比较并非如此.希望有人可以指出我正确的方向!
empty =  "        " 
pawn =   "  Pawn  " 
rook =   "  Rook  " 
knight = " Knight " 
bishop = " Bishop " 
queen =  "  Queen " 
king =   "  King  "
emptyspot = {} 
for i = 1, 8 do
    emptyspot[i] = {}
    for j = 1, 8 do
        emptyspot[i][j] = "    /    "   
    end 
end
function chessBoard() 
    io.write("\n\nWelcome to LuaChess!\n\n") 
    io.write("          1        2        3        4        5        6        7        8    \n") 
    io.write("      *************************************************************************\n") 
    io.write("      *".. empty …我正在寻找一种方法来找出哪个值最接近表中的x并返回该值.
让我们假设,一秒钟,X是x = 15,我们有4个值{12,190,1,18}的表,我如何使它在这种情况下返回第一个键和值?
有没有办法获得表的每个索引值?
例:
local mytbl = {
    ["Hello"] = 123,
    ["world"] = 321
}
我想得到这个:
{"Hello", "world"}
这是片段:
    local t = {}
    t.tt = {}
    function t.xx()
        for i=1,10 do
            t.tt[i] = i
        end
    end
    for i=1,10 do
        print(t.tt[i])
    end
print功能的结果都是.nil为什么所有元素t.tt都是零?
有些人可以帮我理解这段代码吗?
local dev
for _, dev in ipairs(devices) do
        local net
        for _, net in ipairs(dev:get_wifinets()) do
                netlist[#netlist+1] = net:id()
                netdevs[net:id()] = dev:name()
        end
end