[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  table with ipairs problem...

table with ipairs problem...

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Maxhrk   USA  (76 posts)  [Biography] bio
Date Wed 19 Dec 2007 06:13 AM (UTC)
Message


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!
[Go to top] top

Posted by ThomasWatts   USA  (66 posts)  [Biography] bio
Date Reply #1 on Wed 19 Dec 2007 06:40 AM (UTC)

Amended on Wed 19 Dec 2007 06:45 AM (UTC) by ThomasWatts

Message
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'.
[Go to top] top

Posted by Maxhrk   USA  (76 posts)  [Biography] bio
Date Reply #2 on Wed 19 Dec 2007 07:19 AM (UTC)
Message
i have tired the dot method.. turn out there is error 'wrong argumative type' on the same line.
[Go to top] top

Posted by Maxhrk   USA  (76 posts)  [Biography] bio
Date Reply #3 on Wed 19 Dec 2007 07:29 AM (UTC)

Amended on Wed 19 Dec 2007 07:32 AM (UTC) by Maxhrk

Message
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.
[Go to top] top

Posted by ThomasWatts   USA  (66 posts)  [Biography] bio
Date Reply #4 on Wed 19 Dec 2007 08:34 PM (UTC)
Message
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.
[Go to top] top

Posted by Maxhrk   USA  (76 posts)  [Biography] bio
Date Reply #5 on Wed 19 Dec 2007 10:00 PM (UTC)
Message
that is very interesting. thanks.
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


16,636 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]