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

Global variable not working

Started by lifewcody, 03 October 2015 - 10:39 PM
lifewcody #1
Posted 04 October 2015 - 12:39 AM
So I'm basically creating a table on modems and instead of being i1, i2, i3, etc. They are all overwriting each other:


--> Variables <--
local build = 110.00
interfaces = {}
local settings = {
[ "Threat Detection" ] = {
  [ "enabled" ] = false, -- true / false
  [ "pps" ] = 0, -- 0 - 20+
  [ "onOver" ] = "none", -- shun / reject / none
  [ "exclude" ] = {} -- excluded ip's from getting shunned
}
}
--> Functions <--
function logInfo(message)
term.setTextColor(colors.blue)
log(message)
end
function logWarn(message)
term.setTextColor(colors.yellow)
log(message)
end
function logErr(message)
term.setTextColor(colors.red)
log(message)
end
function log(message)
print(message)
end
function DecToBase(val,base)
if val == 0 then return 0 end
local b, k, result, d = base or 10, "0123456789ABCDEFGHIJKLMNOPQRSTUVW",""
while val > 0 do
  val, d = math.floor(val/B)/>/>/>, math.fmod(val,B)/>/>/>+1
  result = string.sub(k,d,d)..result
end
return result
end
function getMac(side)
local sidesTable = {top = 1,bottom = 2,left = 3,right = 4,back = 5,front = 6}
side = tostring(side)
local macBuffer = tostring(DecToBase(os.computerID() * 6 + sidesTable[side],6))
	local MACaddr = string.rep("0",4-#macBuffer).. macBuffer
return MACaddr
end
function openModems()
local sides = rs.getSides()
  for i=1, #sides do
	if peripheral.getType(sides[i]) == "modem" then
  interfaces[("i" .. #interfaces + 1)] = {
   [ "MAC" ] = getMac(sides[i]),
   [ "modem" ] = peripheral.wrap(sides[i]),
   [ "RX" ] = {
	[ "packets" ] = 0,
	[ "errors" ] = 0,
	[ "dropped" ] = 0,
	[ "bytes" ] = 0,
   },
   [ "TX" ] = {
	[ "packets" ] = 0,
	[ "errors" ] = 0,
	[ "dropped" ] = 0,
	[ "bytes" ] = 0,
   },
  }

	  interfaces[("i" .. #interfaces + 1)].modem.open(1)
  
   logInfo("Opened " .. "i" .. #interfaces + 1)

	end
  end
  for k, v in pairs(interfaces) do
print(k .. " > " .. tostring(v))
  end
end
--> Main Loop <--
logInfo("Begin POST")
openModems()

Image:
Edited on 03 October 2015 - 10:53 PM
Bomb Bloke #2
Posted 04 October 2015 - 01:15 AM
Although it isn't related to your stated problem, why do you want to make "interfaces" a global anyway?

The issue's here:

interfaces[("i" .. #interfaces + 1)]

Most table-related functions/features hinge on you treating your table as an array - filled with numeric indexes only. You're using strings as keys to index into your table, like a hash-map. This means that #interfaces will always be 0, because it doesn't count keys such as "i1" when figuring out the table size.

You could manually keep track of the table size yourself (simply define and increment your own counter variable to use instead of #interfaces), but it seems to me that it'd be easier to just stick with numeric indexes (eg, interfaces[#interfaces + 1]).

Don't forget to stopping adding 1 when you go to open/log/whatever the modem you just placed in your table!
Edited on 03 October 2015 - 11:16 PM