如何将数据从现有文件放入表

沃森·波伊

我在ComputerCraft中有一个乌龟程序,该程序打算作为存储单元。我输入了一个项目,它应该读取一个文件以查找放置该项目的位置。如果有新项目,它将信息添加到文件中。当我做

blockTypes = {}

local file = fs.open(blockTable","r")
line = file.readAll()
table.insert(blockTypes,line)
file.close()

信息以字符串形式放入。这意味着我做不到

print(blockTypes[1][1])

并获得通常处于该位置的值。

这是我的代码:

blockAmount = 3
highestVal = {2,0,5}
blockTypes = {}
function addBlock()
    local file = fs.open("blockTable", "a")
    file.write(name)
    file.write(" = {")
    file.write(highestVal[1])
    file.write(",")
    file.write(highestVal[2])
    file.write(",")
    file.write(highestVal[3])
    file.writeLine("};")
    file.close()
end
function getBlock()
    local file = fs.open("blockTable","r")
    line = file.readAll()
    table.insert(blockTypes,line)
    file.close()
end
--This was used to test before implementing the new file system that I am trying to get to work
blockTypesOld = {
    log = {2.0,0,1};
    dirt = {2,0,2};
    cobblestone = {2,0,3};
    iron_ingot = {2,0,4};
    planks = {2,0,5};
}
pos = {0,0,0}
looking = 0
function fuel()
    if turtle.getFuelLevel() < 20 then
        turtle.select(16)
        turtle.refuel(1)
    end
end
function left()
    turtle.turnLeft()
    looking = looking - 1
    if looking < 0 then
        looking = 3
    end
end
function right()
    turtle.turnRight()
    looking = looking + 1
    if looking > 3 then
        looking = 0
    end
end
function forward()
    fuel()
        if turtle.forward() then
            if looking == 0 then
                pos[1] = pos[1] - 1
            elseif looking == 1 then
                pos[3] = pos[3] - 1 
            elseif looking == 2 then
                pos[1] = pos[1] + 1
            elseif looking == 3 then
                pos[3] = pos[3] + 1
            else
            end
        end

end
function up()
    fuel()
    turtle.up()
    pos[2] = pos[2] + 1
end
function down()
    fuel()
    turtle.down()
    pos[2] = pos[2] - 1
end
function goHome()
    while pos[3] > 0 do
        while  looking > 1 do
            left()
        end
        forward()
    end
    while  pos[2] > 0 do
        down()
    end
    while  pos[1] > 0 do
        while  looking > 0 do
            left()
        end
        forward()
    end
end
function goTo(a,b,c)
    goHome()
    while looking < 2 or looking > 2 do
        right()
    end
    for i = pos[1],a do
        forward()
    end
    while looking > 3 or looking < 3 do
        right()
    end
    for i = pos[3],c do
        forward()
    end
    for i = pos[2],b do
        up()
    end
    while looking < 2 or looking > 2 do
        left()
    end
end
while true do
    turtle.select(15)
    while not turtle.suck() do
        sleep(1)
    end
    itemDetails = turtle.getItemDetail()
    --Finding what mod item is from and removing corresponding labels--
    --EX: "minecraft:log" becomes "log"
    if itemDetails.name:match("^ae2stuff:(.+)$") then
        name = itemDetails.name:match("^ae2stuff:(.+)$")
    elseif itemDetails.name:match("^minecraft:(.+)$") then
        name = itemDetails.name:match("^minecraft:(.+)$")
    elseif itemDetails.name:match("^appliedenergistics2:(.+)$") then
        name = itemDetails.name:match("^appliedenergistics2:(.+)$")
    elseif itemDetails.name:match("^buildcraftbuilders:(.+)$") then
        name = itemDetails.name:match("^buildcraftbuilders:(.+)$")
    elseif itemDetails.name:match("^forge:(.+)$") then
        name = itemDetails.name:match("^forge:(.+)$")
    elseif itemDetails.name:match("^buildcraftenergy:(.+)$") then
        name = itemDetails.name:match("^buildcraftenergy:(.+)$")
    elseif itemDetails.name:match("^buildcraftfactory:(.+)$") then
        name = itemDetails.name:match("^buildcraftfactory:(.+)$")
    elseif itemDetails.name:match("^buildcraftsilicon:(.+)$") then
        name = itemDetails.name:match("^buildcraftsilicon:(.+)$")
    elseif itemDetails.name:match("^buildcrafttransport:(.+)$") then
        name = itemDetails.name:match("^buildcrafttransport:(.+)$")
    elseif itemDetails.name:match("^buildcraftcore:(.+)$") then
        name = itemDetails.name:match("^buildcraftcore:(.+)$")
    elseif itemDetails.name:match("^buildcraftlib:(.+)$") then
        name = itemDetails.name:match("^buildcraftlib:(.+)$")
    elseif itemDetails.name:match("^computercraft:(.+)$") then
        name = itemDetails.name:match("^computercraft:(.+)$")
    elseif itemDetails.name:match("^enderstorage:(.+)$") then
        name = itemDetails.name:match("^enderstorage:(.+)$")
    elseif itemDetails.name:match("^extracells:(.+)$") then
        name = itemDetails.name:match("^extracells:(.+)$")
    elseif itemDetails.name:match("^thermaldynamics:(.+)$") then
        name = itemDetails.name:match("^thermaldynamics:(.+)$")
    elseif itemDetails.name:match("^thermalexpansion:(.+)$") then
            name = itemDetails.name:match("^thermalexpansion:(.+)$")
    elseif itemDetails.name:match("^thermalfoundation:(.+)$") then
        name = itemDetails.name:match("^thermalfoundation:(.+)$")
    elseif itemDetails.name:match("^tconstruct:(.+)$") then
        name = itemDetails.name:match("^tconstruct:(.+)$")
    elseif itemDetails.name:match("^webdisplays:(.+)$") then
        name = itemDetails.name:match("^webdisplays:(.+)$")
    elseif itemDetails.name:match("^ironchest:(.+)$") then
        name = itemDetails.name:match("^ironchest:(.+)$")
    else
        print("ERROR MOD NOT FOUND")
    end
    getBlock()
    local elem = blockTypes[name]
    --Gets fuel from fuel chest
    right()
    turtle.select(16)
    turtle.suck(5)
    turtle.select(15)
    if elem then
    --If the item does exist, the turtle goes to it's chest and places it in the chest
        goTo(elem[1]-1, elem[2]-1, elem[3]-1)
        turtle.select(15)
        turtle.drop()
        goHome()
        right()
        turtle.select(16)
        turtle.drop()
        turtle.select(15)
        left()
    else
        --Creates information for new item--
        addBlock()
        blockAmount = blockAmount + 1
        highestVal[3] = highestVal[3] + 1
        if highestVal[3] > 5 then
            highestVal[3] = 1
            highestVal[2] = highestVal[2] + 1
        end
        if highestVal[2] > 4 then
            highestVal[2] = 0
            highestVal[1] = highestVal[1] + 2
        end
        blockTypes[blockAmount] = name
        blockTypes[name] = {highestVal[1],highestVal[2],highestVal[3]}
        local elem = blockTypes[name]
        left()
        turtle.select(15)
        turtle.drop()
        right()
        turtle.select(16)
        turtle.suck(2)
        fuel()
        turtle.drop()

        goTo(1,-1,4)
        --Crafts an Iron Chest for the New Item
        for i = 1,3 do
            turtle.select(i)
            turtle.suck(1)
        end
        turtle.select(5)
        turtle.suck(1)
        turtle.select(7)
        turtle.suck(1)
        for i = 9,11 do
            turtle.select(i)
            turtle.suck(1)
        end
        turtle.select(6)
        turtle.craft()
        goTo(1,-1,3)
        for i = 1,3 do
            turtle.select(i)
            turtle.suck(1)
        end
        turtle.select(5)
        turtle.suck(1)
        turtle.select(7)
        turtle.suck(1)
        for i = 9,11 do
            turtle.select(i)
            turtle.suck(1)
        end
        turtle.select(1)
        turtle.craft()
        goHome()
        right()
        turtle.select(16)
        turtle.suck(5)
        goTo(elem[1]-1, elem[2]-1, elem[3]-1)
        turtle.select(1)
        turtle.place()
        goHome()
    end
end

包含项目坐标的文件称为blockTable,由以下内容组成:

--blockName = {xCoord,yCoord,zCoord};--
oak_stairs = {2.0,0.0,5.0};
iron_ingot = {2.0,0.0,4.0};
turtle = {2.0,0.0,5.0};

当我放入新物品时,它会注册该物品,制作一个箱子并将其放入。当我将同一物品放入时,它会直接进入箱子而无需制作新物品。但是,当我重新启动乌龟并放入相同的物品时,它会创建另一个新的箱子并尝试将其放置在新物品上。我希望它即使重新启动后也能够保留其信息。我一直在努力与这个程序有一段时间了,任何帮助将不胜感激。谢谢!

DarkWiiPlayer

每次添加新块时都读取/写入整个文件似乎有些混乱,特别是如果您考虑到在写入新文件的过程中可能会重启乌龟并最终导致一半数据丢失的话。

您确实有一个文件系统,那么为什么不使用它呢?创建一个目录并为每个块名称编写一个新文件,然后将坐标保存在其中。这也使您可以为每种块类型存储一个以上的箱子,以防万一您变得肮脏的情况比使用海龟挖掘地图的大块块时要早。

我只是意识到我实际上并没有回答这个问题,所以去了:

拥有文件后file,您可以轻松地使用读取一行file.readLine()这将返回它读为字符串的行,或者返回nil您位于文件末尾的行。假设您只在块名称后写了三个用空格隔开的坐标,然后可以将它们解析为一个表,如下所示:

local file = fs.open('chests')
local chests = {}
while true do
  local line = file.readLine()
  if line then
    local name, x, y, z = line:match("(%a+) ([%d.+-]+) ([%d.+-]+) ([%d.+-]+)")
    chests[name] = {
      tonumber(x),
      tonumber(y),
      tonumber(z)
    }
  else
    break
  end
end

将所有这些都包装在一个函数中,以使其更加整洁。

边注:

Computercraft在处理文件方面有些不便。在常规的Lua中,我会这样做:

local function map(f, elem, ...)
    if elem then return f(elem), map(f, ...) end
end

local function readchests(file)
    local res = {}
    for line in io.open(file):lines() do
        local name, x, y, z = line:match("(%a+)"..(" [%d.-+]+"):rep(3))
        res[name]={map(tonumber, x, y, z)}
    end
    return res
end

如果要保持将数据存储为有效的Lua代码的格式,可以使用读取整个文件file.readAll()"return {"在开头和"}"结尾处添加然后仅加载并执行该字符串。总的来说,我不建议这样做。如果整个文件只是可以读取和运行的有效Lua代码,那就更好了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将熊猫数据添加到现有的csv文件中?

如何将新数据追加到现有的配置单元表

如何将JSON数据附加到现有JSON文件node.js

如何将PHP数据放入HTML表

如何将熊猫数据框合并到现有的reportlab表中?

如何将数据附加到现有的Azure数据湖文件中?

如何将Excel数据插入C#中的现有表

如何将数据附加到现有文件

POWER BI-如何将手动列/数据添加到现有表,而不是将列/数据添加到源csv文件

Microsoft Access-如何将数据导入现有表并更新数据

如何将数据和表插入到现有的表中?

如何将R中的数据帧读入现有SQL表中?

如何将数据附加到现有的Excel文件中?

如何将新对象添加到现有实体,将数据放入数据中并让EF将其添加到我的数据库中?

如何将MVC模型类与现有数据库表和架构匹配

如何将数组数据放入表中?

如何将数据附加到现有的Dropbox文件?

如何将sql数据放入html表中

如何将两个表数据放入单列?

如何将文件表与 SQL Server 中的现有表链接

如何将新数据附加到现有文件中

如何将新字段/列添加到现有的 xBase 文件/表

如何将普通表上的现有索引移动到内存优化文件组?

如何将现有文件中的数据放入多维数组 C#

如何将数据添加到表中的现有记录

如何将表数据放入向量/列表?

如何将数据帧附加到磁盘上现有的 Apache Arrow 文件

如何将数据放入 Laravel API 中的表中?

如何将json文件放入熊猫数据框python