Inserting a key and value into a dictionary

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Inserting a key and value into a dictionary

GrahamH
Hi

As a relative new comer  to Lua, an unexpected difficulty I have  
encountered is in inserting a key and value into an existing dictionary.

The table.insert method does the trick for  an indexed value but not  
for a string key. This need seems fundamental, so is there a simple  
solution that I have missed?

Thank you for any help.

Graham
Email: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Inserting a key and value into a dictionary

Alex Queiroz
Hallo,

On 5/17/06, Graham Henstridge <[hidden email]> wrote:
> The table.insert method does the trick for  an indexed value but not
> for a string key. This need seems fundamental, so is there a simple
> solution that I have missed?
>
> Thank you for any help.
>

table[key] = value

--
-alex
http://www.ventonegro.org/
Reply | Threaded
Open this post in threaded view
|

Re: Inserting a key and value into a dictionary

Petite Abeille
In reply to this post by GrahamH

On May 17, 2006, at 13:49, Graham Henstridge wrote:

> The table.insert method does the trick for  an indexed value but not
> for a string key. This need seems fundamental, so is there a simple
> solution that I have missed?

Lua 101:

local aDictionary = {}

aDictionary[ aKey ] = aValue

Programming in Lua
Part I. The Language
Chapter 2. Types and Values
2.5 - Tables

http://www.lua.org/pil/2.5.html

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/

Reply | Threaded
Open this post in threaded view
|

Re: Inserting a key and value into a dictionary

Brian Weed-2
In reply to this post by GrahamH
Graham Henstridge wrote:

> Hi
>
> As a relative new comer  to Lua, an unexpected difficulty I have
> encountered is in inserting a key and value into an existing dictionary.
>
> The table.insert method does the trick for  an indexed value but not
> for a string key. This need seems fundamental, so is there a simple
> solution that I have missed?
>
> Thank you for any help.
>
> Graham
> Email: [hidden email]
>
>
>
dict["key"] = value

Is that what you need?

Brian
Reply | Threaded
Open this post in threaded view
|

Re: Inserting a key and value into a dictionary

Javier Guerra Giraldez
In reply to this post by GrahamH
On Wednesday 17 May 2006 6:49 am, Graham Henstridge wrote:
> Hi
>
> As a relative new comer  to Lua, an unexpected difficulty I have
> encountered is in inserting a key and value into an existing dictionary.

table [key] = value

--
Javier

attachment0 (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Inserting a key and value into a dictionary

Richard Simes
In reply to this post by GrahamH
You have a few options, but basically you just index the table and
make an assignment.
 I'll show examples below:

-- create a new table
t = {"cat", "dog", 1, 2}

t[5] = "hello" -- adds a new string at index 5

t["key"] = "value" -- adds a value with a string key

or, a shorthand of this is:

t.key = "value"


Hope this helps,
Richard
Reply | Threaded
Open this post in threaded view
|

Re: Inserting a key and value into a dictionary

GrahamH
Hi

Sorry, I have a very red face. Thank you all for the help.

Graham


On 17/05/2006, at 10:08 PM, Richard Simes wrote:

> You have a few options, but basically you just index the table and
> make an assignment.
> I'll show examples below:
>
> -- create a new table
> t = {"cat", "dog", 1, 2}
>
> t[5] = "hello" -- adds a new string at index 5
>
> t["key"] = "value" -- adds a value with a string key
>
> or, a shorthand of this is:
>
> t.key = "value"
>
>
> Hope this helps,
> Richard

Reply | Threaded
Open this post in threaded view
|

Re: Inserting a key and value into a dictionary

david herviou
In reply to this post by Richard Simes


Richard Simes wrote:

>
> -- create a new table
> t = {"cat", "dog", 1, 2}
>
> t[5] = "hello" -- adds a new string at index 5
>
> t["key"] = "value" -- adds a value with a string key
>
> or, a shorthand of this is:
>
> t.key = "value"
>
Just for reminder (and very important!):
 >=#t
5
Even if t.key has been added
http://www.lua.org/manual/5.1/manual.html  --> 2.5.5 - The Length Operator

dh

herviou.vcf (590 bytes) Download Attachment