[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Array access indirection
- From: Tim Channon <tc@...>
- Date: Sun, 21 Sep 2008 16:51:33 +0100
Thanks folks including those where I did not reply, all helps.
I do have PIL 2.
Peter Cawley wrote:
>> rt={}
>>
>> rt[var]={[pa1]=va1}
>>
>> rt[var]={[pa2]=va2} -- does not add pair
>>
> You want that last statement to add the key/value pair [pa2]=va2 to
> the table rt[var] ? If so:
>
> rt[var][pa2] = va2
>
> The meaning of your instructions:
>
> rt={} -- Set rt to be an empty table
> rt[var]= {[pa1]=va1} -- Set rt[var] to be a table containing the
> key/pair [pa1] = va1
> rt[var]= {[pa2]=va2} -- Set rt[var] to be a table containing the
> key/pair [pa2] = va2
>
> The last instruction doesn't add a pair to an existing table, because
> it is overwriting the existing table
The magic is as follows and is one combination I did not try because
neither on their own work. Syntax is inconsistent, to do with runtime
type definition.
require"tprint"
var="fred"
pa1="john"
va1="sam"
pa2="ken"
va2="sal"
rt={} -- new empty table
rt[var]={[pa1]=va1} -- hint to Lua is subtable and give a it a pair
rt[var][pa2]=va2 -- Lua now knows it is a table,
--syntax for adding is different!
var="eek" -- change table
rt[var]={[pa1]=va1}
rt[var][pa2]=va2
tprint(rt)
[[
"fred":
"john"="sam"
"ken"="sal"
"eek":
"john"="sam"
"ken"="sal"
]]