Jump to content




Lookup Path in Table


  • You cannot reply to this topic
11 replies to this topic

#1 Selim

  • Members
  • 189 posts
  • LocationHiding in Online Storage

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 by Selim, 23 December 2015 - 06:00 PM.


#2 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 23 December 2015 - 08:27 PM

I have encountered the same problem. Luckily for you I have a solution. This

#3 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

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!' )";
}


#4 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 23 December 2015 - 11:01 PM

View PostTheOddByte, on 23 December 2015 - 10:29 PM, said:

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.

#5 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

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 by Wojbie, 23 December 2015 - 11:12 PM.


#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.

#7 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 23 December 2015 - 11:44 PM

View PostBomb Bloke, on 23 December 2015 - 11:35 PM, said:

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.

#8 Selim

  • Members
  • 189 posts
  • LocationHiding in Online Storage

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.

#9 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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?

#10 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

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 by Dragon53535, 25 December 2015 - 11:23 AM.


#11 Selim

  • Members
  • 189 posts
  • LocationHiding in Online Storage

Posted 26 December 2015 - 02:02 PM

View PostBomb Bloke, on 25 December 2015 - 10:51 AM, said:

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.

View PostDragon53535, on 25 December 2015 - 11:23 AM, said:

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!

#12 Selim

  • Members
  • 189 posts
  • LocationHiding in Online Storage

Posted 27 December 2015 - 05:24 PM

I got it working, for anyone wondering.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users