lua-users home
lua-l archive

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


Here's one way to do what you want.

Regards,
ashley

----

function get_field_names(t)
    local names = {}
    local i,v=next(t,nil)
    while i do
        tinsert(names,i)
        i,v=next(t,i)
    end
    return names
end


>color = { red = 1, green = 5, blue = 6 }

>names = get_field_names( color )

>print(names[1],names[2],names[3])

green   red     blue


> -----Original Message-----
> From: Honglei Tu
> Sent: Tuesday, March 06, 2001 3:47 PM
> Subject: how to get the field names for a table
> 

> If I have a Lua table which is
> color["red" = 1 "green" =5 "blue" =6].
> I wonder how to get the field names and put them
> in a set. I want to get ["red" "green" "blue"].
> I will write a Lua function to get them.
> But is there any way to do that ?
>