[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Userdata question
- From: "Jerome Vuarand" <jerome.vuarand@...>
- Date: Wed, 21 Feb 2007 10:32:28 -0500
Verriere Joseph wrote:
> I'm using LUA with wireshark and my problem is with LUA.
> Here is a part of my code :
>
> ctxId = Field.new("h248.ctx")
>
> extract_com = Listener.new()
>
> function extract_com.packet()
>
> print (ctxId())
>
> test = tostring(ctxId())
> print (test)
> end
>
> I don't understand why with th line: print (ctxId()), it prints every
> context Ids but when I put it as a string, I only get the first one.
> How can I get the others?
"print" prints all the parameters it receives. If your ctxId function
returns several parameters, "print" will print all of them. "tostring"
on the other hand only converts its first parameter to a string.
To have access to all return values of ctxId without knowing how much of
them you have you can put them in a table and use them later:
function extract_com.packet()
local values = { ctxId() }
-- {} is a table constructor, so values is a table
-- then you can iterate over the entries in your "values" table
for i,value in ipairs(values) do
print (i, value)
test = tostring(value)
print (test)
end
-- You can even return all values if you want using unpack:
return unpack(values)
end
Everything is explained here, if you haven't read it you should take
some time to do it:
http://www.lua.org/manual/5.1/
> Also with that:
> table = {}
> test = getmetatable (ctxId())
> table = test.__tostring(ctxId())
> print (table)
>
> I only get the first one.
> How can I have the others?
Unless it's documented, I think you should avoid accessing metatable of
types you didn't wrote yourself. The solution I gave above should work
if all you want is access all the return values of ctxId.