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

Convert a path to a table

Started by InDieTasten, 05 April 2013 - 12:12 PM
InDieTasten #1
Posted 05 April 2013 - 02:12 PM
Hello,

i'm going to create a virtual drive program. and i need to convert the string paths to tables:

function convertPathToTable(sPath)
return "thePartINeedYourHelp"
end

tTestPath = convertPathToTable("/homes/username/movies.idm")
--so tTestPath should be this:
tTestPath[1] = "homes"
tTestPath[2] = "username"
tTestPath[3] = "movies.idm"

i have an idea on how to do this by going through all characters. but i'm sure it's possible to do this shorter and easier with some pattern crap i didn't understand yet.

~InDieTasten
Kingdaro #2
Posted 05 April 2013 - 02:35 PM
Just throw every string grouping that doesn't contain a slash into a table.


local function convert(str)
  local t = {}
  for v in str:gmatch('[^/]+') do -- the matchstring here calls for everything but a /, like "[^@]+" would call for everything but @.
    table.insert(t, v)
  end
  return t
end

testPath = convert('/homes/username/movies.idm') -- because i hate the stupid "bBoolean" or "nNumber" notation with a passion
Engineer #3
Posted 05 April 2013 - 02:40 PM
Hmm.. Check for syntax errors/ errors in general. Im on a phone here. it is also untested


function convert( path )
  local parts = {}
  while path:find('/') do
      local start = path:find( '/' )
      local end = path:find( '/', start )
      local temp
      if start and not end then
          temp = string.sub('/', start)
      else
          temp = string.sub( path, start, end )
          path = path:sub( end - end * 2 )
      end
      table.insert( parts, temp )
   end
  return parts
end

this should get you in the right direction

edit: ninja'd
Hmm…. Kingdaro's method is short and powerful
InDieTasten #4
Posted 21 April 2013 - 11:43 AM
Just throw every string grouping that doesn't contain a slash into a table.


local function convert(str)
  local t = {}
  for v in str:gmatch('[^/]+') do -- the matchstring here calls for everything but a /, like "[^@]+" would call for everything but @.
	table.insert(t, v)
  end
  return t
end

testPath = convert('/homes/username/movies.idm') -- because i hate the stupid "bBoolean" or "nNumber" notation with a passion
thx. didn't figured out how the patters work just yet. so much symblos. watching out for some tutorials here in the forum ;)/>