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

Faster way to check computer label

Started by tvc, 26 November 2015 - 05:53 PM
tvc #1
Posted 26 November 2015 - 06:53 PM
So i am working with a program that changes depending on the label of the pc and i need to know if there is a faster way.

At the moment I am doing something like


name = os.getComputerLabel()
if name == "1" or name == "2" or name =="3" then
...
elseif name =="4" then
...
end
--is it possible to do
c1 =  1,2,3
--and then
if  name == c1 then
...
end
Edited on 26 November 2015 - 05:53 PM
MKlegoman357 #2
Posted 26 November 2015 - 07:43 PM
We could give you a better answer if you'd tell us what exactly you need it for. But with that kind of setup you could do it with tables:


local names1 = {
  ["1"] = true;
  ["2"] = true;
  ["3"] = true;
}

local names2 = {
  ["4"] = true;
  ["5"] = true;
  ["6"] = true;
}

local label = os.getComputerLabel()

if names1[name] then
 ...
elseif names2[name] then
 ...
end

valithor #3
Posted 26 November 2015 - 11:46 PM
Way with less typing:


local function check(...)
  for k,v in pairs({...}) do
    if name == v then
      return true
    end
  end
end

if check(1,2,3) then
elseif check(4) then
end
tvc #4
Posted 27 November 2015 - 02:06 AM
We could give you a better answer if you'd tell us what exactly you need it for. But with that kind of setup you could do it with tables:


local names1 = {
  ["1"] = true;
  ["2"] = true;
  ["3"] = true;
}

local names2 = {
  ["4"] = true;
  ["5"] = true;
  ["6"] = true;
}

local label = os.getComputerLabel()

if names1[name] then
...
elseif names2[name] then
...
end


here ill show u what im working on so you can get an ideal
http://pastebin.com/tEQwApQc
KingofGamesYami #5
Posted 27 November 2015 - 02:38 AM
Just a few FYIs

+ tonumbering a table will always give you nil (line 20)
+ You'll need to close the file handle, nothing can access that file until you close the handle (very problematic if the program is terminated).
+ You'll need to open the file, write the value down, and close the file every single time you move
+ lama automatically tracks turtle movements, documentation is here.
Konlab #6
Posted 29 November 2015 - 08:15 AM
+ You'll need to open the file, write the value down, and close the file every single time you move
No:

local handle = fs.open("xy","w")

local function os.pullEvent(...)
    local event = { os.pullEventRaw(...) }
    if event[1] == "terminate" then
        handle.close()
        error("You pressed CTRL+T",0)
    end
    return unpack(event)
end
...

handle.close()
H4X0RZ #7
Posted 29 November 2015 - 01:35 PM
+ You'll need to open the file, write the value down, and close the file every single time you move
No:

local handle = fs.open("xy","w")

local function os.pullEvent(...)
    local event = { os.pullEventRaw(...) }
    if event[1] == "terminate" then
        handle.close()
        error("You pressed CTRL+T",0)
    end
    return unpack(event)
end
...

handle.close()

What about CTRL+S and CTRL+R?
Konlab #8
Posted 29 November 2015 - 02:28 PM
+ You'll need to open the file, write the value down, and close the file every single time you move
No:

local handle = fs.open("xy","w")

local function os.pullEvent(...)
    local event = { os.pullEventRaw(...) }
    if event[1] == "terminate" then
        handle.close()
        error("You pressed CTRL+T",0)
    end
    return unpack(event)
end
...

handle.close()

What about CTRL+S and CTRL+R?
Shutting down and rebooting automatically closes all file handlers.
My solution also makes the file inaccessible for everybody except the program that has the handler, which is good, too.
Edited on 29 November 2015 - 01:30 PM
KingofGamesYami #9
Posted 29 November 2015 - 03:29 PM
…but this behavior makes it a horrible solution:


local file = fs.open( "wrong", "w" )
file.write( 1 )
file.write( 2 )
os.shutdown()

Contents of wrong after this runs:

12

Did you want 12? No, you wanted 2. But you got 12 because you never overwrote 1, because you never closed and reopened the file handle.