How do I find the first letter of a string?

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: How do I find the first letter of a string?

Post by tsuujin »

Sorry for the late reply.

Easiest way to do this is to write a small parser function. This can either handle the end result by itself, or simply return a table with each number in the "string" as an element. This is nice because tables are much easier to work with than strings for this kind of thing.
Code: [show] | [select all] lua
function parseActions(var)
	local actions = {}
	for i=1,#var do
		actions[i] = tonumber(var:sub(i,i))
	end
	return actions
end
Now, assuming you're just using an alias that matches an all digit line (such as ^\d+$) then the entire matched line would be stored in matches[1]. So, to get the table:
Code: [show] | [select all] lua
local someVar = parseActions(matches[1])
Now you can iterate through that list and work on it.
Code: [show] | [select all] lua
for _,digit in pairs(parseAction(matches[1])) do
    if digit == 1 then something
    elseif digit == 2 then something different
    end
end

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: How do I find the first letter of a string?

Post by Heiko »

@ Tsujin

matches[1] does not contain the whole line, but the complete match. The line on which the match was taken is stored in the Lua variable line.
matches[1+n] with n>0 holds the capture group n.

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: How do I find the first letter of a string?

Post by tsuujin »

Heiko wrote:@ Tsujin

matches[1] does not contain the whole line, but the complete match. The line on which the match was taken is stored in the Lua variable line.
matches[1+n] with n>0 holds the capture group n.
... yes, and ^\d+$ would inherently match the entire line, if only digits were received. Thus, matches[1] works exactly as it should for that purpose.

Post Reply