On lua mailing list somebody asked if there is any way to check very short if an element is in a specific list of elements.

In php you would do for example

1
if (in_array("A",array("A","B","C")) ) { echo "hello"; }

<p style="margin: 5px">Of course, everyone can write a short in_table function in <!--more-->lua:</p>

<pre class="geshi"><span style="color: #000000; font-weight: bold">function</span> in_table <span style="color: #66cc66">(</span> e, t <span style="color: #66cc66">)</span>
<span style="color: #000000; font-weight: bold">for</span> _,v <span style="color: #000000; font-weight: bold">in</span> <span style="color: #a3209f; font-weight: bold">pairs</span><span style="color: #66cc66">(</span>t<span style="color: #66cc66">)</span> <span style="color: #000000; font-weight: bold">do</span>
<span style="color: #000000; font-weight: bold">if</span> <span style="color: #66cc66">(</span>v==e<span style="color: #66cc66">)</span> <span style="color: #000000; font-weight: bold">then</span> <span style="color: #000000; font-weight: bold">return</span> <span style="color: #000000; font-weight: bold">true</span> <span style="color: #000000; font-weight: bold">end</span>
<span style="color: #000000; font-weight: bold">end</span>
<span style="color: #000000; font-weight: bold">return</span> <span style="color: #000000; font-weight: bold">false</span>
<span style="color: #000000; font-weight: bold">end</span></pre>
But Asko answered a clever and very short way:

if ({A=1,B=1,C=1})["A"] then print("hello") end

Why does that work?

{A=1,B=1,C=1} creates nothing but a table with following style:

[A] => 1
[C] => 1
[B] => 1

(used print_r for lua here)

So what does ({A=1,B=1,C=1})["A"] do then? It returns the value of the table's ["A"]-Field. And this is 1! Thatswhy the 'then'-branch will be used.

Basicly its using lua's all table entries are set or nil way to check wether a element is set as key in the temporary table or not.

So if you use your own lua implementation for in_table or do it that tricky way is your choice!