【问题标题】:Lua / Why does adding a new element to a matrix overwrites previously added one?Lua / 为什么向矩阵添加新元素会覆盖先前添加的元素?
【发布时间】:2019-01-08 17:16:55
【问题描述】:

我正在学习 Lua,所以我决定尝试实现一个从数组中获取坐标并输出 ASCII 图片的函数。

只要我没有在一行中放几个点(纵坐标),一切都很好:

它输出这个

...X
.X..
..X.
...X

不是这个

XXXX
.X..
..X.
...X

经过一番调查,我得出结论,除了create_massive(),所有本地功能都运行良好。当它放置一个点时,先前添加在同一 y 轴上的点将被删除。


为了澄清,我是这样得出这个结论的:

我只是删除了函数 create_massive() 并自己定义了其庞大的 drawing

local n=nil

drawing={
    {1,1,1,1},
    {n,1,n,n},
    {n,n,1,n},
    {n,n,n,1},
}

程序输出我想要的。


完整代码

我可以只保留函数 create_massive(),因为只需要修复它,但我认为完整的代码会更有帮助:

function draw(coords,sym,spc)

local sym = sym or "X" -- dot
local spc = spc or "-" -- empty slot

local max={} -- massive containing max co-ordinates
local min={} -- massive containing min co-ordinates
local drawing={} -- massive containing drawing

local function find_min_max()
    -- finds min/max co-ordinates

    for i=1,#coords do
        local c=coords[i]

        if i%2~=0 then
            max.x=max.x and ((c>max.x) and c or max.x) or c
            min.x=min.x and ((c<min.x) and c or min.x) or c
        else
            max.y=min.y and ((c>max.y) and c or max.y) or c
            min.y=min.y and ((c<min.y) and c or min.y) or c
        end
    end

end

local function create_massive()
    -- creates massive containing drawing

    for i=2,#coords,2 do
        local y=coords[i]
        local x=coords[i-1]
        drawing[y]={[x]=1} -- the thing is, it overwrites previous dots' position, so line can contain only the last called dot
    end

end

local function print_drawing()

    local n=1
    local line={}

    for i=min.y,max.y do
        if drawing[i] then
            for k=min.x,max.x do
                if drawing[i][k] then
                    line[n]=line[n] and line[n]..sym or sym
                else
                    line[n]=line[n] and line[n]..spc or spc
                end
                if k==max.x then
                    n=n+1
                end
            end
        else
            for p=min.x,max.x do
                line[n]=line[n] and line[n]..spc or spc
            end
            n=n+1
        end
    end

    for i=1,#line do
        print(line[i])
    end

end

find_min_max()
create_massive() -- probably this function works incorrect
print_drawing()

end

m={1,1,2,1,3,1,4,1,2,2,3,3,4,4} -- co-ordinates: even indexes of the array are y, others are x

draw(m) -- main function is called

【问题讨论】:

    标签: arrays matrix lua overwrite ascii-art


    【解决方案1】:

    drawing[y]={[x]=1} 创建一个包含单个条目的新行。

    试试drawing[y][x]=1

    【讨论】:

    • 我尝试过,但它输出错误:lua5.3:source_file.lua:33:尝试索引一个零值(字段'?')堆栈回溯:source_file.lua:33:在本地'create_massive' source_file.lua:70: 在函数'draw' source_file.lua:77: 在主块 [C]: in ?所以我在drawing[y][x]=1 之前添加了drawing[y]={},但它没有帮助。
    • @ViChyavIn - drawing[y] = drawing[y] or {}; drawing[y][x]=1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多