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

Lookup Path in Table

Started by Selim, 23 December 2015 - 04:44 PM
Selim #1
Posted 23 December 2015 - 05:44 PM
I have a table that has all the files stored on the computer within it (directories being another table), and I want to lookup a path within the table.

For example:
{
fileA = 'data',
dirA = {
 data = 'stuff',
 contents = {
  subFileA = 'data',
  subFileB = 'data',
 },
},
fileB = 'data',
}

If I wanted to use the path '/dirA/subFileA' the table, what would be the best (or at least one) way of doing that?

I have been thinking about it for a while, but I haven't come up with anything yet.
Edited on 23 December 2015 - 05:00 PM
Creator #2
Posted 23 December 2015 - 08:27 PM
I have encountered the same problem. Luckily for you I have a solution. This
TheOddByte #3
Posted 23 December 2015 - 10:29 PM
You could make this easier for you and simply store the full path as a string index and the code or whatever as a value, so that the table would look like this

{
    ["somedirectory/subdirectory/foo"] = "bar";
    ["startup"] = "print( 'Hello World!' )";
}
Creator #4
Posted 23 December 2015 - 11:01 PM
You could make this easier for you and simply store the full path as a string index and the code or whatever as a value, so that the table would look like this

{
	["somedirectory/subdirectory/foo"] = "bar";
	["startup"] = "print( 'Hello World!' )";
}

I remember taking this option into account, but for some reason I don't remember, I opted against it.
Wojbie #5
Posted 23 December 2015 - 11:12 PM
Well same path can be written in few different ways. Like:

somedirectory/subdirectory/foo
somedirectory/subdirectory/dummy/../foo
somedirectory\subdirectory\foo
somedirectory/subdirectory/./foo
somedirectory/./subdirectory\dummy\../foo
All would reach same file.
Edited on 23 December 2015 - 10:12 PM
Bomb Bloke #6
Posted 23 December 2015 - 11:35 PM
shell.resolve() should be able to deal with that, though using subtables would certainly make things easier if you wanted to delete or move a directory.
Creator #7
Posted 23 December 2015 - 11:44 PM
shell.resolve() should be able to deal with that, though using subtables would certainly make things easier if you wanted to delete or move a directory.
Maybe that was the reason. I believe it was.
Selim #8
Posted 24 December 2015 - 03:17 PM
I didn't want to put the full path in the table as the table contains every file, I was thinking it would be easier to organize it more like how directories are organized, within each other.
Bomb Bloke #9
Posted 25 December 2015 - 10:51 AM
So back to your original question: Are you merely asking whether that table structure is the way to go, or are you asking how you'd use a string representing a path to index into that table?
Dragon53535 #10
Posted 25 December 2015 - 11:23 AM
There's sort of a way to do it.

Parse the string into individual table indexes.


local tbl = {"somedirectory","subdirectory","foo"}

and then loop through that table looking at the lookup table to see if you've got your value.


local fileTable = {
fileA = 'data',
dirA = {
data = 'stuff',
contents = {
  subFileA = 'data',
  subFileB = 'data',
},
},
fileB = 'data',
}


local function findFile(path)

  --#Parse function here, I'm too lazy to make it
  local foundValue = fileTable
  for a,v in ipairs(tbl) do --#Loop through the numerical indices in order
	if (foundValue[v]) then--#If it exists.
	  if (type(foundValue[v]) ~= "table") then --If it's not a table, and just an item
		return foundValue[v], a == #tbl and true or false --#first part: return the data. Second part: Tell them if this was the file we're looking for, or we're at a dead end.
	  else --#Then we've got another table
		foundValue = foundValue[v] --#Lets go into that table, since that's part of our path
	  end
	else --#Our file doesn't exist.
	  return nil,nil --#Return absolutely nothing
	end
  end
  return foundValue,true --#If our last value was a table, we're not gonna return it, so we need to do it here.
end

local fileData, madeToEnd = findFile("dirA/contents/subFileA") --#FileData will hold your file/directory (Directory if we ended on contents at this point)
--#or it will have nil, if it couldn't find your file.
--#MadeToEnd will have a value of true,false,or nil. True means it looped as far as it could, and found a file or directory at the last entry: subFileA here.
--#False means it found a value BEFORE the last one, if we did dirA/data/subFileA we would get the stuff from the data entry into fileData and this would be false.
--#Nil means well, it means that fileData was nil as well. Which means it didn't find the file you were looking for.
Edited on 25 December 2015 - 10:23 AM
Selim #11
Posted 26 December 2015 - 02:02 PM
So back to your original question: Are you merely asking whether that table structure is the way to go, or are you asking how you'd use a string representing a path to index into that table?
How to get the data from the table, I am sticking with the table structure.

snip
Ok, thank you!

I think I can figure it out from this point, if not, I will be back. Thanks all who gave input!
Selim #12
Posted 27 December 2015 - 05:24 PM
I got it working, for anyone wondering.