Tables, Functions & varargs

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Tables, Functions & varargs

Andy Bushnell
Hi,

Another (newbie) question here (and I did RTFM first - but it isnt sinking
in yet!:-)

Ok,

I have a function to print the name of a node (a tolua'd data structure)

function printName( node, ... )

	print( node:getName() )

end

I want to be able to call printName with some optional arguments. In my
print example I want to call it with an "indent level" number which the
function can key-off & print appropriate tabs before writing the name as
above.

Another function is supposed to get passed the exact same argument &
increment/decrement the value which will be used in printName.

so at the top level I have code like:

level = 1
printAll(root,level)  where printAll is defined as printAll(node,...)

I believe my level number value is put into a table & passed to varargs
functions as a table. Hence in my vararg functions Ive tried...


	indent_level = arg[1]
	indent_level = unpack(arg) // not sure what/when or why i'd use the unpack
func?????
	indent_level = tonumber(arg[1])


	my intention being that I could then...

	arg[1] = indent_level + 1 --or minus 1


when I try to use any of my indent_level assignments above I get an error in
the for loop (to print the tabs) about indent_level not being a number!!!

Im a bit confused and I couldnt find an example which seems to do what i'm
doing - so any help appreciated!:-)

Thanks

Andy

ps
Lua is very cool though!



Reply | Threaded
Open this post in threaded view
|

Re: Tables, Functions & varargs

Eric Tetz-2
--- Andy Bushnell <[hidden email]> wrote:
> when I try to use any of my indent_level assignments above
> I get an error in the for loop (to print the tabs) about
> indent_level not being a number!!!

I don't see the problem in the code you posted. This will work
fine:

function printAll (node, ...)
  print(arg[1])
  print(type(arg[1]))
end

level = 1
printAll("foo", level)


Here's a simple example of what (I think) you're trying to do
(though varargs aren't necessary in this example). Maybe it will
help:

function indent(n)
  for i=1,n do io.write(' ') end
end

function printTable (t, ...)
  for k,v in t do
    indent(arg[1])
    if type(v) == "table" then
      print(k)
      printTable(v, arg[1] + 2)
    else
      print(k.." = "..v)
    end
  end
end

a = {
  ba = {},
  bb = {
    ca = { "da", "db", "dc", },
    cb = {
      da = { "ea" },
    },
  },
}

printTable (a, 1)


__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

Reply | Threaded
Open this post in threaded view
|

SV: Tables, Functions & varargs

Andreas Stenius-3
Maybe this isn't what you're looking for and it doesn't do varargs, but I've
found it quite usefull, and might give some ideas..

====8<----
-- an extended ?table.foreach' kind of function..
function tfe( t )
    local level = 0
    local tables = {}
    tables[ t ] = true

    local function f( key, val )
        print( string.rep( "\t\t", level ) .. key, val )
        if( type(val) == "table" ) then
            if( tables[ val ] ~= true ) then
                tables[ val ] = true
                level = level + 1
                table.foreach( val, f )
                level = level - 1
            else
                print( "\t*recursion*" )
            end
        end
    end

    table.foreach( t, f )
end
------>8========

Example use:
>tfe( _G )
or
>tfe{ my_table, some_other_table, foo_table } -- notice that I've
 still only passed one argument to tfe..


//Andreas


> > when I try to use any of my indent_level assignments above
> > I get an error in the for loop (to print the tabs) about
> > indent_level not being a number!!!
>
> I don't see the problem in the code you posted. This will work
> fine:
>
> function printAll (node, ...)
>   print(arg[1])
>   print(type(arg[1]))
> end
>
> level = 1
> printAll("foo", level)
>
>
> Here's a simple example of what (I think) you're trying to do
> (though varargs aren't necessary in this example). Maybe it will
> help:
>
> function indent(n)
>   for i=1,n do io.write(' ') end
> end
>
> function printTable (t, ...)
>   for k,v in t do
>     indent(arg[1])
>     if type(v) == "table" then
>       print(k)
>       printTable(v, arg[1] + 2)
>     else
>       print(k.." = "..v)
>     end
>   end
> end
>
> a = {
>   ba = {},
>   bb = {
>     ca = { "da", "db", "dc", },
>     cb = {
>       da = { "ea" },
>     },
>   },
> }
>
> printTable (a, 1)
>



Reply | Threaded
Open this post in threaded view
|

RE: Tables, Functions & varargs

Andy Bushnell
In reply to this post by Andy Bushnell
Hi,

Ok, I think I have the varargs/tabke thing working -but- how do I do the
following?

Say I have a table t = {6,7,8} and this is what I want to pass to a
function. This func will pass t on to another func and so on (ie a number of
times).

How do I get one of the funcs to modify its passed-in vararg tables so that
it sets arg[2] from (what was) 7 to (say) 9. Having done this when it passes
the arg table onto another func - the called func should get a table with
{ 6,9,8 } as a param.

In C/C++ Speak (which might be easier to explain) I have an array or
structure...

struct MyInfo
{
	int x;
	int y;
	int z;
	MyInfo()
	{
		x = 6;
		y = 7;
		z = 8;
	}
};

void MyFunc( void* anyArg )
{
	MyInfo* pArg = (MyInfo*)anyArg;

	if( pArg->y == 7 )
	{
		printf("change arg\n');
		pArg->y = 9;
		return MyFunc( anyArg );  // <== Pass on arg with its changed value
						  // syntax for how to accomplish this in lua im not sure about
						  // also syntax for passing varargs between chains of calling
						  // funcs.
	}
	else
		printf("arg already changed\n');
	return 1;
}

use like:

MyInfo info;
MyFunc( (void*)(&info) );

All help appreciated.

Andy




-----Original Message-----
From: Andy Bushnell [[hidden email]]
Sent: Thursday, October 09, 2003 10:17 PM
To: [hidden email]
Subject: Tables, Functions & varargs


Hi,

Another (newbie) question here (and I did RTFM first - but it isnt sinking
in yet!:-)

Ok,

I have a function to print the name of a node (a tolua'd data structure)

function printName( node, ... )

	print( node:getName() )

end

I want to be able to call printName with some optional arguments. In my
print example I want to call it with an "indent level" number which the
function can key-off & print appropriate tabs before writing the name as
above.

Another function is supposed to get passed the exact same argument &
increment/decrement the value which will be used in printName.

so at the top level I have code like:

level = 1
printAll(root,level)  where printAll is defined as printAll(node,...)

I believe my level number value is put into a table & passed to varargs
functions as a table. Hence in my vararg functions Ive tried...


	indent_level = arg[1]
	indent_level = unpack(arg) // not sure what/when or why i'd use the unpack
func?????
	indent_level = tonumber(arg[1])


	my intention being that I could then...

	arg[1] = indent_level + 1 --or minus 1


when I try to use any of my indent_level assignments above I get an error in
the for loop (to print the tabs) about indent_level not being a number!!!

Im a bit confused and I couldnt find an example which seems to do what i'm
doing - so any help appreciated!:-)

Thanks

Andy

ps
Lua is very cool though!



Reply | Threaded
Open this post in threaded view
|

RE: Tables, Functions & varargs

Eric Tetz-2
--- Andy Bushnell wrote:
> Say I have a table t = {6,7,8} and this is what I want to pass to
a function.
> This func will pass t on to another func and so on (ie a number
of times).
> 
> How do I get one of the funcs to modify its passed-in vararg
tables so that
> it sets arg[2] from (what was) 7 to (say) 9. Having done this
when it passes
> the arg table onto another func - the called func should get a
table with {
> 6,9,8 } as a param.

This is where you would use that 'unpack' function you were
wondering about earlier. Unpack simply returns every value in a
list:

t = {6, 7, 8}
a, b, c = unpack(t)
print(a, b, c) -- output:  6   7   8

So you can pass a vararg list to another vararg function by
unpacking it:

function a(...)
  print(unpack(arg))
end

function b(...)
  arg[2] = 9
  a(unpack(arg))
end

b(6,7,8)


__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com