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

It should work

Started by MayContainVennom, 06 August 2014 - 12:10 AM
MayContainVennom #1
Posted 06 August 2014 - 02:10 AM
Heyo,
So I started this project that will enable me to get a listing of the items that pass through my spawner adding it to a text file which can later be reviewed online (Apache virtual host pointing to the file) or using monitors in game. I wrote the code along with a friend of mine and we got it working on a 2 dimensional array. Everything seems to be working but the fact that every time it Adds an item to the array that is already there it adds a new line for it. We have looked through the code and cannot see a reason for this to be happening. So I have come to you :)/>



The items enter the chest on the right from the spawner. After the system resets the Itemducts are activated using a rednet cable (Brown) after pulling for 10 seconds the computer changes it's output to black then proceeds to index the items that are in the chest in front of it (Using open peripherals). After indexing it rights to the file then activates Orange allowing the items to flow into the enderchest then starts over again.

c = peripheral.wrap("container_chest_3")
file = items
redN = colors.brown -- Rednet On colour
redNO = colors.black -- Rednet Off colour
EnderC = colors.orange -- Colour left on Enderchest when counting
redNS = "right" -- Rednet cable side
redNE = colors.orange

itemFile = {}
itemFile[1] = {}
itemFile[1][1] = 0
itemFile[1][2] = 0

function getItems()
	  for i = 1,27  do
	 itemArray = {}
	  if c.getStackInSlot(i) then
		print("See if anythings in there")
		 data = c.getStackInSlot(i)
		 amount = table.getn(itemFile)
		  for i = 1,amount do
			if itemFile[i][1] == data.name then
			print("Is in the array")
				itemFile[i][2] = itemFile[i][2] + data.qty
			else
				print("Not in table D:")
				print(data.name..data.qty)
				table.insert(itemArray,data.name)
				table.insert(itemArray,data.qty)
				for key,value in ipairs(itemFile) do
										print(key, value)
						end
				table.insert(itemFile,itemArray)
			end
		 end
	  end
   end
end

function writeFile(line)
  local diskFile = fs.open("disk/items","w")
  amount = table.getn(itemFile)
  print(amount)
  for i = 2,amount do
	text = itemFile[i][2].." X " ..itemFile[i][1]
	print(itemFile[i][1]," ",itemFile[i][2])
	diskFile.writeLine(text)
  end
	diskFile:close()
end

while true do
  print("Setting EnderChest")
  redstone.setBundledOutput(redNS,redN)
  print("Tranfering Items")
  sleep(10)
  redstone.setBundledOutput(redNS,redNO)
  getItems()
  writeFile()
  print("Written to file")
  redstone.setBundledOutput(redNS,redNE)
  sleep(10)
  redstone.setBundledOutput(redNS,redNO)
  print("item's sent to the ME")
end

Any help is appricated,
Thanks a lot,
Vennom.
Edited on 06 August 2014 - 12:25 AM
Neywiny #2
Posted 06 August 2014 - 03:20 AM
well I'll tell you what, I'd like to help you. this build is something I'd probably do eventually, so I figured why not help a brother out. Anyways, when this happens to me, I print everything physically possible to the computer. I'm talking print the data.name, the amount variable, anything that changes when it finds a new item, print it to a file or monitor or what have you just be able to read it. anything at all. then, look at the result. You should see a number or two not changing. Also keep in mind that I go overboard for debugging, yet am very curious in what you're doing since it's an actual "real world" application, as apposed to my creative-superflat builds. Please report back, and I'm serious. I want to know how it turned out and If I can help in any other way.

side note-is that the faithful texture pack?
MayContainVennom #3
Posted 06 August 2014 - 03:26 AM
well I'll tell you what, I'd like to help you. this build is something I'd probably do eventually, so I figured why not help a brother out. Anyways, when this happens to me, I print everything physically possible to the computer. I'm talking print the data.name, the amount variable, anything that changes when it finds a new item, print it to a file or monitor or what have you just be able to read it. anything at all. then, look at the result. You should see a number or two not changing. Also keep in mind that I go overboard for debugging, yet am very curious in what you're doing since it's an actual "real world" application, as apposed to my creative-superflat builds. Please report back, and I'm serious. I want to know how it turned out and If I can help in any other way.

side note-is that the faithful texture pack?

Yes it is faithful. We ran some prints for debugging (Some of them are still in there) and the code is working completly fine. The problem being is that instead of overriding the line in the array it is creating another. For example If I put in 1 Steak on the first cycle and 2 on the second it would display it as:
3 X Steak
1 X Steak

When it should be:
3 X Steak

All of the arrays are cleared at the right time and as far as I can tell, we are adding the items correctly if it is already in the array.
Edited on 06 August 2014 - 01:29 AM
Neywiny #4
Posted 06 August 2014 - 03:29 AM
well then it's time to go the backup route. I hope we didn't have to do this (imagine in your mine the lua god telling you "there is a way, but no, it's too dangerous!") no adding to the table. try having a fixed 27-row table that stores it. then just print the lines that aren't nil. It's the only way I can think of without a world download or equivalent viewing as it is an actual application of a program
MayContainVennom #5
Posted 06 August 2014 - 03:31 AM
There must be a better way. I'll give you the server details in PM if you want to have a look. I'm GMT and should be asleep but I really want to get it sorted so I'll jump back on if you want
Neywiny #6
Posted 06 August 2014 - 03:34 AM
well I love servers and will check it out but timing may be an issue due to me supposing to be asleep soon :/ anyways try my way. It's the lazy way, but in all honesty the output is the same all you'd do is check to see if there's data in that row's table, then print it if there is
MayContainVennom #7
Posted 06 August 2014 - 03:37 AM
Having the 27 row array would work for a while but the aim is to have everything that we spawn on the screen at all times. It wouldn't be long until we need more and it will still need to be two dimensional so the values can be updated. I'll PM you the info now
flaghacker #8
Posted 06 August 2014 - 09:23 AM
You have 2 nested for-loops with the same value, "i". I don't know if that's technically a problem - they could be conflicting - but it's certainly confusing.
Edited on 06 August 2014 - 07:24 AM
wieselkatze #9
Posted 06 August 2014 - 11:17 AM
I'll take a look on that when I'm at home. Though the first thing I saw is, that you create your item table 27 times right now. I assume it's intended to be outside the loop?
Also I wouldn't use a table with numbers as indices, instead with strings.
In that way you could do something like that:

yourTable[item.rawName] = yourTable[item.rawName] and yourTable[item.rawName] + item.amount or item.amount
That way you would never be able to create a new index for the same item.
As for the saving, you could just use a pairs loop and you're done.
Sorry for typos, I wrote that on my mobile.
MayContainVennom #10
Posted 06 August 2014 - 11:35 AM
You have 2 nested for-loops with the same value, "i". I don't know if that's technically a problem - they could be conflicting - but it's certainly confusing.

Treid, it still seems to be happening
Edited on 06 August 2014 - 09:40 AM
wieselkatze #11
Posted 06 August 2014 - 12:10 PM
Voila! There is the code. (I finally arrived at home :P/>)


c = peripheral.wrap("container_chest_3")
file = items
redN = colors.brown -- Rednet On colour
redNO = colors.black -- Rednet Off colour
EnderC = colors.orange -- Colour left on Enderchest when counting
redNS = "right" -- Rednet cable side
redNE = colors.orange

itemFile = {}

function getItems()
local items = c.getAllStacks()

  for k, v in pairs(items) do
	itemFile[v.name] = itemFile[v.name] and itemFile[v.name] + v.qty or v.qty
  end
end

function writeFile(line)
  local diskFile = fs.open("disk/items","w")

  for k, v in pairs(itemFile) do
	diskFile.write(k.." x "..v.."\n")
  end

  diskFile:close()
end

while true do
  print("Setting EnderChest")
  redstone.setBundledOutput(redNS,redN)
  print("Tranfering Items")
  sleep(1)
  redstone.setBundledOutput(redNS,redNO)
  getItems()
  writeFile()
  print("Written to file")
  redstone.setBundledOutput(redNS,redNE)
  sleep(1)
  redstone.setBundledOutput(redNS,redNO)
  print("item's sent to the ME")
end

Much shorter and does the job.
So now no item should be duplicated etc..
Let me know if something still doesn't behave like it should :)/>
Edited on 06 August 2014 - 10:12 AM
MayContainVennom #12
Posted 06 August 2014 - 12:22 PM
Voila! There is the code. (I finally arrived at home :P/>)


c = peripheral.wrap("container_chest_3")
file = items
redN = colors.brown -- Rednet On colour
redNO = colors.black -- Rednet Off colour
EnderC = colors.orange -- Colour left on Enderchest when counting
redNS = "right" -- Rednet cable side
redNE = colors.orange

itemFile = {}

function getItems()
local items = c.getAllStacks()

  for k, v in pairs(items) do
	itemFile[v.name] = itemFile[v.name] and itemFile[v.name] + v.qty or v.qty
  end
end

function writeFile(line)
  local diskFile = fs.open("disk/items","w")

  for k, v in pairs(itemFile) do
	diskFile.write(k.." x "..v.."\n")
  end

  diskFile:close()
end

while true do
  print("Setting EnderChest")
  redstone.setBundledOutput(redNS,redN)
  print("Tranfering Items")
  sleep(1)
  redstone.setBundledOutput(redNS,redNO)
  getItems()
  writeFile()
  print("Written to file")
  redstone.setBundledOutput(redNS,redNE)
  sleep(1)
  redstone.setBundledOutput(redNS,redNO)
  print("item's sent to the ME")
end

Much shorter and does the job.
So now no item should be duplicated etc..
Let me know if something still doesn't behave like it should :)/>

Seems to be working! Thanks a lot man.
natedogith1 #13
Posted 07 August 2014 - 05:17 AM
The issue seems to be that in your for loop.

		amount = table.getn(itemFile)
		for i = 1,amount do
			if itemFile[i][1] == data.name then
				print("Is in the array")
				itemFile[i][2] = itemFile[i][2] + data.qty
			else
				print("Not in table D:")
				print(data.name..data.qty)
				table.insert(itemArray,data.name)
				table.insert(itemArray,data.qty)
				for key,value in ipairs(itemFile) do
					print(key, value)
				end
				table.insert(itemFile,itemArray)
			end
		 end
The else will get run every time there's an item that isn't the one you're looking for, as appose to only if there isn't the item you're looking for.
You should instead do something along the lines of


		local create=true
		for i = 1,#itemFile do
			if itemFile[i][1] == data.name then
				print("Is in the array")
				itemFile[i][2] = itemFile[i][2] + data.qty
				create=false
			end
		end
		if create then
			print("Not in table D:")
			print(data.name..data.qty)
			table.insert(itemArray,data.name)
			table.insert(itemArray,data.qty)
			for key,value in ipairs(itemFile) do
				print(key, value)
			end
			table.insert(itemFile,itemArray)
		end

also, you shouldn't be using 'table.getn(tbl)' instead you should be using '#tbl'
Function table.getn corresponds to the new length operator (#); use the operator instead of the function.