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

Non deterministic result between computer screen and monitor

Started by Piratdu52, 24 November 2014 - 10:46 PM
Piratdu52 #1
Posted 24 November 2014 - 11:46 PM
I write some programs to manage a network of essentia pipe for my Thaumcraft needs. Recently i have write one to show what aspect i have on a big screen. But my program dont show aspect in the same order (i have "ordered" my table alphabetically )on the computer screen and on the monitor. The computer screen show "Free x0 Ignisx48 Perditio x48" (it's fine) when the monitor show "Ignis x48 Perditio x48 Free x0" (it's wrong). I don't understand why, if you can solve this, I would appreciate.

This is my code

local monitor=nil

function setMonitor()
  monitor=peripheral.wrap("top")
end

function openNetwork()
  rednet.open("left")
end

function split(str, pat)
  local t={}
  local fpat="(.-)"..pat
  local last_end=1
  local s,e,cap = str:find(fpat,1)
  while s do
	if s~= 1 or cap~= "" then
	  table.insert(t,cap)
	end
	last_end=e+1
	s,e,cap= str:find(fpat,last_end)
  end
  if last_end <= #str then
	cap= str:sub(last_end)
	table.insert(t,cap)
  end
  return t
end

function runMonitor()
  openNetwork()
  setMonitor()
  while true do
	local tableAspect={Free=0}
	rednet.broadcast("howmany")
	local timer=os.startTimer(0.2)
	local continue=true
	while continue do
	  local event,param1,message=os.pullEvent()
	  if (event=="rednet_message") then
		local messageTable=split(message,"-")
		--write(textutils.serialize(messageTable))
		if (messageTable[1]=="aspectdata") then
		  if(messageTable[2]=="untyped") then
			tableAspect["Free"]=tableAspect["Free"]+1
		  else
			if(tableAspect[messageTable[2]]==nil) then
			  tableAspect[messageTable[2]]=messageTable[3]
			else
			  tableAspect[messageTable[2]]=tableAspect[messageTable[2]]+messageTable[3]
			end
		  end
		end
	  elseif(event=="timer") then
		if(timer==param1) then
		  continue=false
		end
	  end
	end
	--write(textutils.serialize(tableAspect))
	local x,y=monitor.getSize()
	table.sort(tableAspect)
	monitor.clear()
	local i=1
	for k, v in orderedPairs(tableAspect) do
	  --my issue is here
	  write(" "..k.." x"..v) --this works fine
	  monitor.write(k.." x"..v)-- this dont works
	  monitor.setCursorPos(1,i)
	  i=i+1
	end
	os.sleep(5)
  end
end

function __genOrderedIndex(t)
  local orderedIndex={}
  for key,value in pairs(t) do
	table.insert(orderedIndex,key)
  end
  table.sort(orderedIndex)
  return orderedIndex
end

function orderedNext(t,state)
  if state==nil then
	t.__orderedIndex=__genOrderedIndex(t)
	key=t.__orderedIndex[1]
	--write(textutils.serialize(t.__orderedIndex))
	return key, t[key]
  end
  key=nil
  for i=1,table.getn(t.__orderedIndex) do
	if t.__orderedIndex[i]==state then
	  key= t.__orderedIndex[i+1]
	end
  end
  if key then
	return key, t[key]
  end
  t.__orderedIndex=nil
  return
end

function orderedPairs(t)
  return orderedNext, t, nil
end

runMonitor()
--write(textutils.serialize(split("test-1","-")))

Thank you in advance for the help.
KingofGamesYami #2
Posted 24 November 2014 - 11:57 PM
The only thing I can see is that you are setting the monitor cursor after writing on it. Try changing that.
Piratdu52 #3
Posted 25 November 2014 - 12:05 AM
:)/> Thanks it was that.