Jump to content




Convert a path to a table


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

#1 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 05 April 2013 - 01: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

#2 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 05 April 2013 - 01: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


#3 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 05 April 2013 - 01: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

#4 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 21 April 2013 - 10:43 AM

View PostKingdaro, on 05 April 2013 - 01:35 PM, said:

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 ;)





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users