lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org]
> On Behalf Of Michal Kolodziejczyk
> Sent: Thursday, March 10, 2011 10:11 AM
> To: lua-l@lists.lua.org
> Subject: Re: How to extract a word at a given position in a string?
> 
> > What I am currently doing is even more brutal than that: I know that
> I
> > look for a file path. So I iterate over all words, and check for each
> of
> > them if the file exists. If true, I found a file and I can act on it
> J.
> > But there is surely a better way…
> 
> OMG, for each string you make a system call :( Your hard drive does not
> like this for sure.

[BG] I've improved on this: I know that I print only absolute file paths, so I check for file existence only on words with a '\\':

			local fileUnderCaret
			if selectedLine then
				for segment, sep in string.split( selectedLine, "[ \t\n]") do
					-- let's see if this looks like a file name before actually checking its existence
					if string.find( segment, "\\", 1, true) then
						local filemode = lfs.attributes( segment, "mode")
						if filemode == "file" then
							fileUnderCaret = segment
						end
					end
				end
			end

After all, clicking on the line is good enough for me, I never have more than 1 file name in it. Somehow I find it more elegant than extracting the word character by character and then concatenating this character list.

Benoit.