1

I have the following 3 functions:

function getName() -- returns filename without ext local files = scandir(currentDir()) local name = nil for i = 1, #files do if isValidExt( getExt(files[i]) ) then name = getFilename(files[i]) break end end return name end 

 

function currentDir() local string = debug.getinfo(1).source local endPoint = getLastOcurrence(string, '/') local dir = string.sub(string, 2, endPoint) return dir end 

 

function getLastOcurrence(str, char) local last = string.find(string.reverse(str), char, 1, true) return #str - last end 

The strange thing is that debugging ends without issues, while running gives the error:

...\Downloader.lua:22: attempt to perform arithmetic on local 'last' (a nil value) stack traceback: ...\Downloader.lua:22: in function 'getLastOcurrence' ...\Downloader.lua:39: in function 'currentDir' ...\Downloader.lua:50: in function 'getName' 

What could cause such a difference between debug and run?

1 Answer 1

1

My guess is that when you are not in debug mode you compile your code and strip debug information, so debug.getinfo(1).source is not a filename (and does not contain a / character). Try to print its value in the currentDir() function.

Sign up to request clarification or add additional context in comments.

4 Comments

I dont think that's the issue here, I think the interpreter auto-converts all '/' to '\' anyways since I'm building from Windows.
Maybe my tentative explanation of why there is no / in the filename is wrong but the problem is certainly that there is no / in the filename. Do what I said: add print(string) after the local string = debug.getinfo(1).source line. Another possibility is that you are running from the directory where the source file is...
Anyway, there is no / in the string you pass to getLastOcurrence, which crashes when char is not in str (because, as the error message says, last is nil).
Yeah you were right I added string = string.gsub(string, '\\', '/') before calling getLastOcurrence and now it works, thanks. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.