Nice, thanks you very much for explanation!kikito wrote:Come on guys. Lua's patterns have always been difficult. Give vlad a break.
Besides, getting the pattern is actually tricker than it looks. BlackBulletV, who is very experienced with Lua, got it wrong (his pattern will fail with files with a double extension, such as myfile.tar.gz).
Here's what I'd use to get the file name only (this is what vlad needs - probably there's a simpler pattern, but this one works very well):Here's an extended version (compatible with the previous one) which does a little more; it returns the corresponding extension:Code: Select all
function getBasename(filename) return filename:match("^([^%.]*)%.?") -- "myfile.lua" -> "myfile" end
Tests:Code: Select all
function getBasenameAndExtension(filename) return filename:match("^([^%.]*)%.?(.*)$") -- "myfile.lua" -> "myfile", "lua" end
Result:Code: Select all
for _,filename in ipairs({"myfile.lua", "myfile", ".bashrc", "myfile.tar.gz", "myfile.yeah-baby.yeah"}) do print(("getBasename(%q) = %q"):format(filename, getBasename(filename))) print(("getBasenameAndExtension(%q) = %q, %q"):format(filename, getBasenameAndExtension(filename))) end
Code: Select all
getBasename("myfile.lua") = "myfile" getBasenameAndExtension("myfile.lua") = "myfile", "lua" getBasename("myfile") = "myfile" getBasenameAndExtension("myfile") = "myfile", "" getBasename(".bashrc") = "" getBasenameAndExtension(".bashrc") = "", "bashrc" getBasename("myfile.tar.gz") = "myfile" getBasenameAndExtension("myfile.tar.gz") = "myfile", "tar.gz" getBasename("myfile.yeah-baby.yeah") = "myfile" getBasenameAndExtension("myfile.yeah-baby.yeah") = "myfile", "yeah-baby.yeah"
Read the contents of folder
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 8
- Joined: Mon Oct 01, 2012 7:44 pm
Re: Read the contents of folder
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Read the contents of folder
This is unfortunate, we have wildly different views here, then. Tar.gz is perhaps the only double extension I do care about, otherwise it's generally just a dot in the filename. Also, I wouldn't say the extension of ".bashrc" is "bashrc".kikito wrote:his pattern will fail with files with a double extension, such as myfile.tar.gz
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Read the contents of folder
To be completely honest it's the same case to me - in LÖVE I doubt I'll ever need any other "composed" file extension. But if I get a .tar.gz, I want the thing to work intuitively and remove the gz and the tar too, dammit!bartbes wrote:Tar.gz is perhaps the only double extension I do care about
In ruby on rails I there is a lot more of double/triple extension: index.html.erb and script.js.coffee.erb are not uncommon.
I don't have a strong opinion about that; It was simpler to implement that way, so I did. I doubt many of LÖVERs will have to deal with .dotfiles anyway. The test is there to ensure that the functions work without error in all cases (i.e. not raising errors or returning nils) not to express a particular personal preference.bartbes wrote:Also, I wouldn't say the extension of ".bashrc" is "bashrc".
When I write def I mean function.
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: Read the contents of folder
Ah yes, you are correct. Never thought about that since I never deal with double extensions these days.kikito wrote:Besides, getting the pattern is actually tricker than it looks. BlackBulletV, who is very experienced with Lua, got it wrong (his pattern will fail with files with a double extension, such as myfile.tar.gz).
EDIT: Just for the hell of it, here's how to extract the filename from a path with folders:
Code: Select all
path:match("([^/]+)$")
("foo/bar/image.png"):match("([^/]+)$") -- image.png
Who is online
Users browsing this forum: No registered users and 6 guests