This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
bigbaddevil6's profile picture

Loading A Table For Editing And Saving Again

Started by bigbaddevil6, 24 August 2013 - 08:08 PM
bigbaddevil6 #1
Posted 24 August 2013 - 10:08 PM
So currently what Im trying to do is have a Miscperipherals items scan blocks and give the value (thats where the os.pullEvent("isort_item") comes from). I want it to store the Id's in the Table defined in the code have it saved. and then when another item comes through have it open it up for adding in the items into the list and saving again. The problem im having is its not wanting to load the table again I feel like all the pieces are there im just not putting them together correctly. I dont want a rewrite just need told on what i need to add/change to make it work



m = peripheral.wrap("bottom")

tItemList = {1, 2, 3} -- temp placeholders

function sort()  -- use MiscPeripheral for interactive sorter
event, item, amount = os.pullEvent("isort_item")
print("Item: "..item.."  Amount: "..amount)
  load()
  table.insert(tItemList, item)
  m.sort(2)
end

function save()
  local file = fs.open("itemList", "w")
  file.write(textutils.serialize(tItemList))
  file.close()
end

function load()
  local file = fs.open("itemList", "r")
  local data = file.readAll()
  file.close()
  return textutils.unserialize(data)
end


while true do
  sort()
  save()
  for i = 1, 4 do
	print(tItemList[i])
  end
end
jay5476 #2
Posted 24 August 2013 - 11:07 PM
this is what I picked up from your code

file = fs.open("someFile", "w")
 -- w(write) mode will erase everything in the current file so it would be better to do
file = fs.open("someFile", "a")
 -- writes to the end of the file but i think when using write you have to start with \n for a new line
bigbaddevil6 #3
Posted 25 August 2013 - 01:29 AM
Well my problem is the table wont load in need to be loaded an opened so it can be written over by new information and have checks to make sure the id isn't already in there.
immibis #4
Posted 25 August 2013 - 01:50 AM
That's because when you load the table, you don't do anything with tItemList.
You load the table, forget about the table you just loaded, then continue with the old table.
bigbaddevil6 #5
Posted 25 August 2013 - 02:37 AM
How would u connect the two cause the ways i have tried never worked out all the tables ive worked with were changing values of predetermined states. This will be my first needing to add to a list of value on the spot and having it stored for future altering
LBPHacker #6
Posted 25 August 2013 - 03:38 AM
Solution #1: Put the table returned by load() into tItemList instead of just calling load():
tItemList = load()
Solution #2: In function load(), put the table returned by textutils.unserialize() directly into tItemList:
tItemList = textutils.unserialize(file.readAll()) -- no need of local keyword here, tItemList is an upvalue
As immibis mentioned, the only problem with your code is that although you load the table of your dreams from the file, you don't assign it to anything. Tada, garbagecollect.
bigbaddevil6 #7
Posted 25 August 2013 - 12:54 PM
alright i decided to go with the 2nd solution and put it into the load function it now it just says "Bad Argument: table excpected, got nil" when it goes to insert a new item into the table. feel like a nub
LBPHacker #8
Posted 25 August 2013 - 02:09 PM
That means textutils.unserialize returned nil, which means itemList didn't contain a serialized table. I forgot to mention; when you store configuration (or for that matter, any other kind of) data in a file, there is a chance that the user deletes that file. In this case, you should use default values. I might not be clear here, so I'll just show the code:
itemList = textutils.unserialize(file.readAll()) or {}
-- * the value on the left of the OR might be nil; in that case
--   we'll get the value on the right - that is a default value

Here I'll show how the code would look like if I had written it.
SpoilerPastebin: pastebin get DskBxs7P smonitor
-- * actually, no need of wrapping up the sorter,
--   it'll fire the isort_item event anyways
-- * and I'll be using peripheral.call for .sort
-- local sorter = peripheral.wrap("bottom") -- * you can uncomment this if you want

local itemList

local function saveItemList()
    local handle = fs.open("itemList", "w")
    handle.write(textutils.serialize(itemList))
    handle.close()
end

local function loadItemList()
    if not fs.exists("itemList") then
        itemList = {} -- * default value
        saveItemList()
    end
    local handle = fs.open("itemList", "r")
    itemList = textutils.unserialize(handle.readAll())
    handle.close()
end

loadItemList()

while true do
    local event, item, amount = os.pullEvent("isort_item")
    print("Got " .. tostring(amount) .. " pieces of item #" .. tostring(item) .. "!")
    table.insert(itemList, item)
    saveItemList()

    -- * the only method of the sorter we're using
    --   is .sort - why wrapping it up then?
    peripheral.call("bottom", "sort", 2)
    -- sorter.sort(2) -- * you can uncomment this if you want

    --[[ -- * I've put this into a comment block
    --      * to be honest, I don't see the point of this part;
    --        is this here only for the sake of debugging?
    for i = 1, 4 do print(itemList[i]) end
    --]] -- * you can uncomment this if you want
end
bigbaddevil6 #9
Posted 25 August 2013 - 03:04 PM
Ahh thanks. To make sure that i have the understood correctly the difference is that

1. you assigned the output of the file to the table itself and if there is no table make one with nothing in it.
2. There is no need to do a peripheral wrap cause its already in the OS.Event
3. A little difference in the way it outputs to screen( Yes that For Loop at the bottom was for debugging to see if it saved correctly)

hopefully I got that right. One thing you forgot was the table.insert() I went ahead and put it in. Seems to work exactly how I wanted it to. May I add you as a friend for future references when I derp?
LBPHacker #10
Posted 25 August 2013 - 04:47 PM
Yup to all.
May I add you as a friend for future references when I derp?
Sure. And yeah, hell, I forgot the most important part! Shame on me… :D/> Gonna edit it for the record.