Guest User

Untitled

a guest
Sep 8th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.21 KB | None | 0 0
  1. local function idToLabel(id)
  2. local computerid = {0, 1, 2} --add more id's here as needed
  3. local label = {"computer 0's label", "computer 1's label", "computer 2's label"} --change these to match, make sure entry 0 is matches 0 above
  4. local found = 0
  5.     for n = 0,#computerid do --starting a for loop to check the above array entires from 0 to the number of entries in computerid (#computerid)
  6.         if computerid[n] == id then --when there's a match continue
  7.             name = label[n] --set variable "name" to the value of the matching label
  8.             found = 1 -- placeholder variable to bypass a nil exception
  9.             return name --return the variable name to where it was called from
  10.         end
  11.     end
  12.     if found = 0 then --if no id and matching label are found
  13.         return false    -- function returns false value
  14.     end
  15. end
  16.  
  17. rednet.open("top")
  18. while true do
  19.     id, text = rednet.receive() --simplified, if () has no number the listener stays open
  20.     idtolabel(id)
  21.     if not idtolabel then   --this line will check if idtolabel returned false
  22.     print(id .. "> " .. text)  --if false instead of using "name" which would be nil, display the id instead
  23.     else                        --otherwise
  24.     print(name .. "> " .. text) --use the name variable rather than the computer id
  25. end
Advertisement
Add Comment
Please, Sign In to add comment