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

Help me! file formats

Started by Konlab, 16 May 2014 - 03:47 PM
Konlab #1
Posted 16 May 2014 - 05:47 PM
Help me with file formats, how can I detect for example .bmp ending filename?
Other can I do, but the endings are problems.
Sorry for bad english
apemanzilla #2
Posted 16 May 2014 - 08:20 PM
Assuming you already have the filename:

function getExtension(filename)
  if filename:find("$.+(%..+)^") then
    local a,b,c = filename:find("$.+(%..+)^")
    return c
  end
end
Konlab #3
Posted 16 May 2014 - 08:58 PM
Sorry, I don't understand anything.
Please tell me what line what does.
Agoldfish #4
Posted 16 May 2014 - 08:59 PM
Sorry, I don't understand anything.
Please tell me what line what does.
It detects everything after a period in a file name.
Konlab #5
Posted 16 May 2014 - 09:01 PM
And why $.+(%..+)
.?
Lignum #6
Posted 16 May 2014 - 09:02 PM
And why $.+(%..+)
.?
It's a pattern.
Konlab #7
Posted 16 May 2014 - 09:05 PM
Uh
if IDon'tUnderstandAnything() then
print("go away")
else
print("stay here")
end
Output:
go away
apemanzilla #8
Posted 16 May 2014 - 09:42 PM
Alright. I'll break it down:
The ": starts a string, as patterns are strings.
The $: means that the string must START with the following pattern
The .: matches any character
The +: allows the previous case to be repeated (match any characters.
The %.: matches for a REAL period
The (: starts a capture (an additional return value if the containing case was matched)
The .+ again, matches all characters
The ): closes the capture
The ^: means the string must END with this pattern.

In short it looks for something like this:
(Any text).(any text) and returns the second set of parenthasees's contents.
Lignum #9
Posted 16 May 2014 - 09:56 PM
The $: means that the string must START with the following pattern.
The ^: means the string must END with this pattern.
Are you sure it's not the other way around? Because your code only seems to work for me when I swap the two.

Your code could also be shortened to a one-liner:

function getExtension(filename)
  return filename:match("^.+(%..+)$")
end
Konlab #10
Posted 17 May 2014 - 06:56 AM
And what is filename:find
KingofGamesYami #11
Posted 17 May 2014 - 02:10 PM
And what is filename:find
http://lua-users.org/wiki/StringLibraryTutorial
TyDoesMC #12
Posted 18 May 2014 - 12:45 AM
You need to stop complaining about our answers and read up on lua.