lua-users home
lua-l archive

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


On this day of 03/14/2008 08:33 AM, Alexandr Leykin saw fit to scribe:
> Sorry, I am still very weak and in terminology does not accustomed
> there, ( "a" "b" "c") set of values with numeric keys, but not set
> Key empty values.
> 
> The effects of Python ...   :-(
> 
> I need quick verification function that is the existence of values in 
> the table:
> is_value(table,value)->boolean
> 
> Now I use a full select:
> function is_value(t,v)
>     for _,tv in pairs(t) do
>         if  tv==v then return true end
>     end
>     return false
> end

Hi Alexandr,

Is there something else going on that makes the suggested solutions not
work? To repeat one of them:

> a = {["a"] = true, ["b"] = true, ["c"] = true, ["d"] = true}
> 
> Then,
> a.a => true (present in table)
> a.f => nil (not present in table)


If you need the array order as well, you can always add something twice
to the table: once treating it as an array, and once as a key.

e.g.

table.insert(a, "z")
a["z"] = true



now a.z --> true, and "z" was added to the end of the array. Iterating
over ipairs won't get you the "z" key, but it will get you the index
containing the value "z".

Cheers,
- David

-- 
~David-Haley
http://david.the-haleys.org