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

turning/merging strings into numbers, then back into strings

Started by hugeblank, 28 November 2015 - 04:22 AM
hugeblank #1
Posted 28 November 2015 - 05:22 AM
Hello fellow/pro that has stumbled upon this post!
I was wondering if there was any feasible way to take a variable (disk_), and merge a number onto it (disk_0). I know it could easily be hard coded, it would just take some time. But I want to see if you can run a loop for it. Say you have a database of disk drives, and you want to store a file in the drive that has the smallest number. You'd want to find a drive that has free space on it, and isn't empty. How in the heck would you do that?
valithor #2
Posted 28 November 2015 - 06:41 AM
Well, lets say you have a table (database) of disk drives:

--# if you do it this way you will have to make sure the drives are in the order you want them to be checked in
local drives = {
  0,
  1,
  2,
  3
}

--# If you just wanted to grab all drives:
local drives = {}
for k,v in pairs({peripheral.find("drive")}) do --# loops through all of the drives found
  if v.isDiskPresent() then
	table.insert(drives,v.getMountPath()) --# if you only wanted the number from it you could do something like: table.insert(drives,string.match(v.getMountPath(),"%d+"))
  end
end

Then we just loop through our table, merge the number on, check to see if it has enough space, and is not empty.

for k,v in ipairs(drives) do --# loops through drives table
  local curDrive = "disk_"..v
  if fs.getFreeSpace(curDrive) > 2000 then --# getting amount of free space, and making sure it is over a certain amount
	if #fs.list() ~= 0 then  --# making sure it isn't empty
	  print(curDrive.." is the lowest disk with files and has free space.")
	  break --# stopping the loop
	end
  end
end
Edited on 28 November 2015 - 05:41 AM
hugeblank #3
Posted 28 November 2015 - 07:09 AM
Ok, will this work if the database/drives are through a wired modem?
Bomb Bloke #4
Posted 28 November 2015 - 07:29 AM
The command valithor's using to detect the drive, peripheral.find("drive"), doesn't care how they're connected.