lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

I'v found another useful function, IMHO worthy including in the library:

function table.find(t, value)
    for k,v in t do
        if v == value then return k end

With respect,

----- Original Message -----
From: "Tom Spilman" <>
To: "Lua list" <>
Sent: Thursday, June 26, 2003 8:47 PM
Subject: Re: table.copy

> >From: "Tobias Käs" <>
> >
> > (Better don't consider coroutines, or userdata which represents a
> > connection... thats getting too complex. There might be other objects
> which
> > cannot be copied at all, and a reference copy might not be desired
> either.)
>     You seem to insist that to have a function it must solve *every*
> situation that could ever occur.  I don't think that should the criteria
> adding functions to the library.  You can always implement your own for
> complex situations, but the library should provide reasonable
> > There is no "general" solution, as your "or" shows, there are (at least)
> > two.
>     =)
> > A recursive table is a table like this:
> >
> > a = {}
> > a.ref = a
> >
> > Those tables can appear in many different forms, and at least I use them
> > quite often, for example in double linked userdata and tables.
> of
> > userdata refers to table; table to userdata)
>     I do too and run into too many garbage collection problems because of
> them.
> > In the function you have given, recursion would lead to an infinite
> I
> > can think of at least three ways to handle this:
> > - don't copy recursive tables (thats what I will do since it is the
> simplest
> > way ;-)
> > - keep the reference to the original recursive table (which breaks the
> > recursion, but still simple)
> > - make a copy which is recursive in the new table (could be complex)
>     Recursive tables are a more difficult issue.  Just not supporting
> recursive tables is probably the best decision that can be made.
> the problem would cost enough that it should probably not be bothered
> Deep copies of recursive tables would just give you a stack overflow
> > Moreover, you cannot predict if in case of nested tables you want to
> a
> > reference or deep copy of the table. Often it will _not_ be enough to
> a
> > general decision for the whole copy process.
> > And here another example: how to copy metatables?
>     Not sure about how often you would be copying complex data structures
> like that.  My needs for copy have always been to create a new instance of
> deep structure of simple lua types.  Maybe a new metatable member __copy
> which can return a correct copy of the data would solve it.  It would
> provide for special cases while still allowing an easy way to copy simple
> data.  It may even be good enough to extend table.copy() into a more
> copy().
> > Of course you could add a metatable function to every table and
> > which decides how to copy it, but that might lead to a lot of
> > work (which you wanted to avoid ;-) and blow up lua scripts.
>     It just wouldn't be a requirement to have it.  If a __copy is present
> is executed to return an appropriate copy of the table/userdata/etc else
> normal functionality of copy would take place.
> > I am still of the opinion that it is the best (and fastest) solution to
> make
> > a function of your own, since you know which features you need. I doubt
> you
> > can insert any hard to find bugs, since its easy to test with "simple"
> > tables.
>     I guess everyone should get back to writing their own library of
> standard functions. =)
>     Tom