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

if label is in table and more

Started by makerimages, 28 September 2012 - 10:13 AM
makerimages #1
Posted 28 September 2012 - 12:13 PM
hey, id like a code to check if a disks label is stored in a a table and then to get its location in the table and get the data in that location in a second table and save it to a variable and then eject disk and do shell.run().
OmegaVest #2
Posted 28 September 2012 - 03:32 PM
Ah, what? Let me see if I can break this down a bit.

You want us (ie the people who are meant to check code, not create it) to make a program that does the following.
Detects a disk (like with os.pullEvent())
Checks that disk's label (I don't know this can actually be done. . . I'm looking into it)
Compares that label to a table, such as user list (if lbl == lblTbl[v])
Run the code stored in the spot in said table at said index (then loadstring(lblTbl[lbl])).
Then ejects the disk (peripheral.wrap(side), disk.eject())

Okay, so I think I have it. Actually, none of this is too hard, you could probably do it yourself with what I've given you and a look at the Lua reference document.

So, some pseudocode to get you started, whereafter you can come back and ask us for advice, bugfixes (assuming you've tried it yourself) and so forth. But, please, do not use the Ask the Pros section for something you don't want to try yourself.

disk = peripheral.wrap(side)
while true do
   pullEvent

   if a disk is injected
	  get disk''s lbl (literally disk.getLabel(), who knew?)
	  for i, v in pairs(lblTbl) do
		 if the disk''s label (lbl) is v
			 function lblFunc = loadtring(lblTbl[v])
			 disk.eject()
		 end
	   end
	end
end


So, go at it. I've given you the really hard code, you just need to figure out the other bits.
jag #3
Posted 28 September 2012 - 04:47 PM
You can use this function to check if contant is in a table:
function table.contains(table, element)
  for _, value in pairs(table) do
    if value == element then
      return true
    end
  end
  return false
end
And to check if a disk is inserted you can just check if the directory DISK exists with
fs.exists("disk")
jag #4
Posted 28 September 2012 - 04:52 PM
I just made a simple script, that will operate like this:
• if theres a disk inserted:
↳ • check if the disk got its name in a table
↓ ↳ • copy the programs from the disk
↓ ↳ • eject the disk
↓ ↳ • run its program
↓ • end
• end
But the disk must have a folder named program where it got it's program(s), and inside that folder, the file that will be executed must be named startprog.
here's the code:
Spoiler
diskList = {
  "superDisk",
  "makerimages",
  "boringDisk",
  "disk with spaces",
  "virus",
  "goCart"
}

function table.contains(table, element)
  for _, value in pairs(table) do
	if value == element then
	  return true
	end
  end
  return false
end

-- Main program

local diskSide = "bottom"

if not fs.exists("loadedDisks/") then
  shell.setDir("/loadedDisks/")
end

while true do
  sleep(.3)
  if fs.exists("disk") then
	if table.contains(diskList, disk.getLabel(diskSide)) then
	  if fs.exists("/loadedDisks/"..disk.getLabel(diskSide)) then
		fs.delete("/loadedDisks/"..disk.getLabel(diskSide)) else
		shell.setDir("loadedDisks/"..disk.getLabel(diskSide)) end
	  fs.copy("/disk/program/", "/loadedDisks/"..disk.getLabel(diskSide))
	  disk.eject(diskSide)
	  shell.run("loadedDisks/"..disk.getLabel(diskSide).."/startprog")
	end
  end
end
Also on pastebin: Jw36BFRt

A little bit messy, but it will do the job
makerimages #5
Posted 29 September 2012 - 09:57 AM
good, but ill have 2 tables and so if the disk label is in t1 i want the data in t2 on the lable`s location in t1 to be read too.
jag #6
Posted 29 September 2012 - 12:16 PM
good, but ill have 2 tables and so if the disk label is in t1 i want the data in t2 on the lable`s location in t1 to be read too.
What? I don't understand what you mean?
You really need to make you sentences more understandable!
OmegaVest #7
Posted 30 September 2012 - 12:06 AM
Jag, he's looking for a program that looks for the disk's label in one table, then, if it finds it, uses the index of that label in a second table, then runs that code.


So, something akin to this (supposing you have the label).


function runLblProg(dskLbl, tUno, tDos)
   for k, v in pairs(tUno) do
      if v = diskLbl then
         shell.run("DiskProgs/" .. tDos[k])
      end
   end
end

Although to be honest, this methods sucks.