0

I have a ComputerCraft program set to turn on a siren when any non-whitelisted players are near:

sensor = peripheral.wrap("top")
function arraysubset(a, b)
   local s = set(b)
   for _, el in pairs(a)
      if not s[el] then
         return false
      end
   end
   return true
end
function sirenOn() rs.setBundledOutput("back",colors.blue) end
function sirenOff() rs.setBundledOutput("back",0) end
while 1 do
  playersNear = sensor.getPlayerNames()
  allowedPlayers = {"VirtualDXS","jettrom","Shad0wlurker16","Demonicmobster","FireFang0113","riggs135","DaisySnow123","MasterAlex930"}
  if playersNear[1] ~= nil then
    if arraysubset(playersNear,allowedPlayers) then sirenOff() else sirenOn() end
  else sirenOff() end
end

However, on line 3 I get an attempt to call nil. This makes me think that the set() function is not present on computercraft. I'm wondering:

  1. Is there another (maybe better) way to find if array a is a subset of array b and
  2. If not where can I get an API with the set() function?
Dessa Simpson
  • 1,232
  • 1
  • 15
  • 30
  • There's no `set` function/datatype in vanilla Lua, nor in computercraft as far as I am aware. Implement it yourself: `local s = {}; for _,v in ipairs(arr) do s[v] = true; end; return s` – Colonel Thirty Two Jul 29 '15 at 16:58
  • I got the set code from this answer: http://stackoverflow.com/a/28302698/3042952 – Dessa Simpson Jul 29 '15 at 17:00

1 Answers1

0

Rereading the source for the subset() code, I am seeing that the code I used required more code from earlier in the answer:

function set(list)
   local t = {}
   for _, item in pairs(list) do
       t[item] = true
   end
   return t
end
Dessa Simpson
  • 1,232
  • 1
  • 15
  • 30