table with ipairs problem...

Posted by Maxhrk on Wed 19 Dec 2007 06:13 AM — 6 posts, 24,634 views.

USA #0


local aliaslist={
	[1] = {enable=true, 
	group="common", 
	label="common", 
	alias_rex=rex.new("^Install$"),
	 alias_func=function () aliases.install(match) return true end
	}
}

function aliasline(input)
	local bool = false
	for id, v in ipairs(aliaslist) do
		if aliaslist[id]["enable"] == true then
			local match
			_, _, match = aliaslist[id].alias_rex:tfind(input)
			if match then
				bool = aliaslist[id]['alias_func'](match)
				return bool
                	end --if
		end --if
	end -- for
end -- aliasline


i ran this code.. and it gave the error like this:

[ILua]: ./trigger.lua:36: bad argument #1 to 'tfind' (string expected, got table)
Traceback:
 [C]:-1: in function 'tfind'
 ./trigger.lua:36: in function 'aliasline'
 lusternia.lua:19: in a Lua function
Error in the 'client_aliases' callback.


so.. basically i am not good at table thing.. but.. i hope to find solution to this problem soon. thanks!
USA #1
Try 'aliaslist[id].alias_rex.tfind(input)' instead. Notice the '.'(dot) versus the ':'(colon), it makes a big difference.
See: http://www.lua.org/manual/5.1/manual.html#2.5.8
The part about syntactical sugar is important.

Also, as a side note, 'id' is the current key of ipairs(aliaslist), and 'v' is the value.
Everywhere inside the for loop you have 'aliaslist[id]' you can safely substitute 'v'.
Amended on Wed 19 Dec 2007 06:45 AM by ThomasWatts
USA #2
i have tired the dot method.. turn out there is error 'wrong argumative type' on the same line.
USA #3
nevermind..

i tries one higher further.


alias:process(input)


when i try check type within trigger() function. it show that input is a table. However.. when i change it bit..


alias.process(input)

voila! the type of 'input' is now string!

harrah.

something to learn. thanks.

edit:

i think alias_rex:tfind is better method because it will result into table. i think. so that way i can get wildcards within (match) table.
Amended on Wed 19 Dec 2007 07:32 AM by Maxhrk
USA #4
Read what I posted in my previous post and I will try to elaborate a little more here.
In your code listed in the OP, 'aliaslist' is a table.
I am assuming 'alias_rex' is a table as well.
I am also assuming 'input' is a string when passed to 'aliasline'.
Both 'tfind' and 'alias_func' are functions in the 'aliaslist' table.
Now the following are both equal to the same in Lua:
alias_rex:tfind(input) == alias_rex.tfind(alias_rex, input)
Therefore, the first argument to 'tfind' is 'alias_rex' and not 'input' as you wanted to be.
Also, to use your last example:
alias:process(input)
IS ALSO THE SAME AS
alias.process(alias, input)
Notice the order of the arguments.
USA #5
that is very interesting. thanks.