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

Returning empty table?

Started by EveryOS, 24 March 2016 - 03:34 PM
EveryOS #1
Posted 24 March 2016 - 04:34 PM
I am making an apis file to later share, but this function I made keeps returning an empty table.


--apis--
tPath = function( sFile )
  if string.sub( sFile, 0, 1) == '/' then
	sFile = ':' .. sFile
  end
  if string.sub(sFile, 0, 2) ~= ':/' then
	sFile = ':/' .. sFile
  end
  if string.sub(sFile, #sFile- 1, #sFile) ~= '/' then
	sFile = sFile .. '/'
  end
  local lastSlash = 0
  local folder = {}
  i=0
  while i ~= #sFile do
	if string.sub(sFile, i, i+1)=='/' then
	  table.insert(folder, string.sub(sFile, lastSlash, i+1))
	  lastSlash = i
	end
  i=i+1
  end
  return folder
end

--test file--
os.loadAPI('apis')
args = {...}
for n, string in ipairs( apis.tPath(args[1])) do
  print(string)
end

--command--
test main/test/somepath
KingofGamesYami #2
Posted 24 March 2016 - 04:48 PM
Why not use string.gmatch?


args = {...}
for dir in args[1]:gmatch( "(.-)/" ) do
  print( dir )
end

This won't print the program name, but that's simple to get with string.match.
EveryOS #3
Posted 24 March 2016 - 05:24 PM
Why not use string.gmatch?


args = {...}
for dir in args[1]:gmatch( "(.-)/" ) do
  print( dir )
end

This won't print the program name, but that's simple to get with string.match.
I had converted a function I once used in javascript to lua. I dunno why it stopped working after I converted it to lua.
moTechPlz #4
Posted 24 March 2016 - 08:17 PM
Hi, i don't know what the function needs to do actually but i think this line is wrong

if string.sub( sFile, i, i+1 ) == '/' then

if string.sub( sFile, i, i )== '/' then
Edited on 24 March 2016 - 07:19 PM