lua-users home
lua-l archive

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


My horrific code:

function StringScanner.prototype:scan( inPattern )
local theStart, theEnd, s1, s2, s3, s4, s5, s6, s7, s8, s9 = string.find( self.contents, inPattern, self.position )
	if theStart ~= self.position then
		return nil
	else
		self.position = theEnd + 1
return string.sub( self.contents, theStart, theEnd ), s1, s2, s3, s4, s5, s6, s7, s8, s9
	end
end


In Ruby, I could cleanly do the above like this...

class StringScanner
	def scan( inPattern )
theStart, theEnd, *matches = string.find( self.contents, inPattern, self.position )
		if theStart != self.position then
			return nil
		else
			self.position = theEnd + 1
			return string.sub( self.contents, theStart, theEnd ), *matches
		end
	end
end

...where the 'splat' operator (*) takes all extra values and turns them into an array when used as an LValue, and explodes the array into its component values when used as an RValue.

Is there a clean way to do the above (catch, save, and then later return) in Lua? (I had hoped I could use a vararg expression magically in place of "*matches" above, but obviously that doesn't work.