creating keys

I’d like to get the values from an indexed table and set those values as the keys in another table. For example,
[lua]
array = {“dave”,“robin”,“michael”,“becca”}
dictionary = {}
for i, #array do
dictionary.array[i] = {}
dictionary.array[i].aNewKey = “whatever”…array[i]
dictionary.array[i].anotherNewKey = “something else”
end
[/lua]
I want to then be able to access the data in like manner:
[lua]
print (dictionary.dave.aNewKey)
[/lua]

I know this should be simple, but it’s the end of the day and I think I’m having a brain fart. Any ideas on how to create a dictionary in this way? [import]uid: 120914 topic_id: 36857 reply_id: 336857[/import]

Hi there,

I think what you wrote is pretty close to what need. In your lines 4-6, replace [lua]dictionary.array[i][/lua] with [lua]dictionary[array[i]][/lua].

You might also find it helpful to read this post, which talks about the difference between the dot notation and the bracket notation: http://developer.coronalabs.com/forum/2013/02/14/using-variables-when-retrieving-field-table

Hope this helps!

  • Andrew [import]uid: 109711 topic_id: 36857 reply_id: 145134[/import]

hey there,

the method closer to “The Lua Way” is by taking advantage of the two functions which iterate through tables, ipairs() and pairs(). one is used for tables as arrays and the other is used for tables as dictionaries. i remember the difference since the “i” in ipairs() will give you integers or indexes of values (of arrays).

the great thing about them is that they give you “both sides” of a table at the same time ( e.g., the key AND value ). it also saves on the need to write the array indexing everywhere. e.g. array[i].

[lua]
local array = {“dave”,“robin”,“michael”,“becca”}
local dictionary = {}

for i,v in ipairs( array ) do
– i contains the index (eg, 1, 2, 3, …)
– v contains the value (eg, ‘dave’, ‘robin’ …)
print(i,v)

– here is your version modified
dictionary[v] = {}
dictionary[v].aNewKey = “whatever”…v
dictionary[v].anotherNewKey = “something else”
– this is another, more concise version
–[[
dictionary[v] = {
aNewKey = “Whatever”…v,
anotherNewKey = “something else”
}
–]]

end

print( dictionary.dave.aNewKey )
[/lua]

[lua]
– output

1 dave
2 robin
3 michael
4 becca
Whateverdave
[/lua]

cheers,
dmc [import]uid: 74908 topic_id: 36857 reply_id: 145156[/import]

Thanks @aukStudios and @dmccusky, thank you for that. The key seemed to be using square brackets instead of dot notation.

dmccuskey, the reason I didn’t use ipairs is because of an article I read a while back reviewing the performance between ipairs/pairs and an iterative loop. The site was located at http://trac.caspring.org/wiki/LuaPerformance, though that appears to be dead now. Basically, the summary is that ipairs and pairs takes about 2x as long to run as an iterative loop. If anyone knows of another source comparing the performance, that would be great to see. [import]uid: 120914 topic_id: 36857 reply_id: 145223[/import]

Hi there,

I think what you wrote is pretty close to what need. In your lines 4-6, replace [lua]dictionary.array[i][/lua] with [lua]dictionary[array[i]][/lua].

You might also find it helpful to read this post, which talks about the difference between the dot notation and the bracket notation: http://developer.coronalabs.com/forum/2013/02/14/using-variables-when-retrieving-field-table

Hope this helps!

  • Andrew [import]uid: 109711 topic_id: 36857 reply_id: 145134[/import]

hey there,

the method closer to “The Lua Way” is by taking advantage of the two functions which iterate through tables, ipairs() and pairs(). one is used for tables as arrays and the other is used for tables as dictionaries. i remember the difference since the “i” in ipairs() will give you integers or indexes of values (of arrays).

the great thing about them is that they give you “both sides” of a table at the same time ( e.g., the key AND value ). it also saves on the need to write the array indexing everywhere. e.g. array[i].

[lua]
local array = {“dave”,“robin”,“michael”,“becca”}
local dictionary = {}

for i,v in ipairs( array ) do
– i contains the index (eg, 1, 2, 3, …)
– v contains the value (eg, ‘dave’, ‘robin’ …)
print(i,v)

– here is your version modified
dictionary[v] = {}
dictionary[v].aNewKey = “whatever”…v
dictionary[v].anotherNewKey = “something else”
– this is another, more concise version
–[[
dictionary[v] = {
aNewKey = “Whatever”…v,
anotherNewKey = “something else”
}
–]]

end

print( dictionary.dave.aNewKey )
[/lua]

[lua]
– output

1 dave
2 robin
3 michael
4 becca
Whateverdave
[/lua]

cheers,
dmc [import]uid: 74908 topic_id: 36857 reply_id: 145156[/import]

Thanks @aukStudios and @dmccusky, thank you for that. The key seemed to be using square brackets instead of dot notation.

dmccuskey, the reason I didn’t use ipairs is because of an article I read a while back reviewing the performance between ipairs/pairs and an iterative loop. The site was located at http://trac.caspring.org/wiki/LuaPerformance, though that appears to be dead now. Basically, the summary is that ipairs and pairs takes about 2x as long to run as an iterative loop. If anyone knows of another source comparing the performance, that would be great to see. [import]uid: 120914 topic_id: 36857 reply_id: 145223[/import]

Hi there,

I think what you wrote is pretty close to what need. In your lines 4-6, replace [lua]dictionary.array[i][/lua] with [lua]dictionary[array[i]][/lua].

You might also find it helpful to read this post, which talks about the difference between the dot notation and the bracket notation: http://developer.coronalabs.com/forum/2013/02/14/using-variables-when-retrieving-field-table

Hope this helps!

  • Andrew [import]uid: 109711 topic_id: 36857 reply_id: 145134[/import]

hey there,

the method closer to “The Lua Way” is by taking advantage of the two functions which iterate through tables, ipairs() and pairs(). one is used for tables as arrays and the other is used for tables as dictionaries. i remember the difference since the “i” in ipairs() will give you integers or indexes of values (of arrays).

the great thing about them is that they give you “both sides” of a table at the same time ( e.g., the key AND value ). it also saves on the need to write the array indexing everywhere. e.g. array[i].

[lua]
local array = {“dave”,“robin”,“michael”,“becca”}
local dictionary = {}

for i,v in ipairs( array ) do
– i contains the index (eg, 1, 2, 3, …)
– v contains the value (eg, ‘dave’, ‘robin’ …)
print(i,v)

– here is your version modified
dictionary[v] = {}
dictionary[v].aNewKey = “whatever”…v
dictionary[v].anotherNewKey = “something else”
– this is another, more concise version
–[[
dictionary[v] = {
aNewKey = “Whatever”…v,
anotherNewKey = “something else”
}
–]]

end

print( dictionary.dave.aNewKey )
[/lua]

[lua]
– output

1 dave
2 robin
3 michael
4 becca
Whateverdave
[/lua]

cheers,
dmc [import]uid: 74908 topic_id: 36857 reply_id: 145156[/import]

Thanks @aukStudios and @dmccusky, thank you for that. The key seemed to be using square brackets instead of dot notation.

dmccuskey, the reason I didn’t use ipairs is because of an article I read a while back reviewing the performance between ipairs/pairs and an iterative loop. The site was located at http://trac.caspring.org/wiki/LuaPerformance, though that appears to be dead now. Basically, the summary is that ipairs and pairs takes about 2x as long to run as an iterative loop. If anyone knows of another source comparing the performance, that would be great to see. [import]uid: 120914 topic_id: 36857 reply_id: 145223[/import]

Hi there,

I think what you wrote is pretty close to what need. In your lines 4-6, replace [lua]dictionary.array[i][/lua] with [lua]dictionary[array[i]][/lua].

You might also find it helpful to read this post, which talks about the difference between the dot notation and the bracket notation: http://developer.coronalabs.com/forum/2013/02/14/using-variables-when-retrieving-field-table

Hope this helps!

  • Andrew [import]uid: 109711 topic_id: 36857 reply_id: 145134[/import]

hey there,

the method closer to “The Lua Way” is by taking advantage of the two functions which iterate through tables, ipairs() and pairs(). one is used for tables as arrays and the other is used for tables as dictionaries. i remember the difference since the “i” in ipairs() will give you integers or indexes of values (of arrays).

the great thing about them is that they give you “both sides” of a table at the same time ( e.g., the key AND value ). it also saves on the need to write the array indexing everywhere. e.g. array[i].

[lua]
local array = {“dave”,“robin”,“michael”,“becca”}
local dictionary = {}

for i,v in ipairs( array ) do
– i contains the index (eg, 1, 2, 3, …)
– v contains the value (eg, ‘dave’, ‘robin’ …)
print(i,v)

– here is your version modified
dictionary[v] = {}
dictionary[v].aNewKey = “whatever”…v
dictionary[v].anotherNewKey = “something else”
– this is another, more concise version
–[[
dictionary[v] = {
aNewKey = “Whatever”…v,
anotherNewKey = “something else”
}
–]]

end

print( dictionary.dave.aNewKey )
[/lua]

[lua]
– output

1 dave
2 robin
3 michael
4 becca
Whateverdave
[/lua]

cheers,
dmc [import]uid: 74908 topic_id: 36857 reply_id: 145156[/import]

Thanks @aukStudios and @dmccusky, thank you for that. The key seemed to be using square brackets instead of dot notation.

dmccuskey, the reason I didn’t use ipairs is because of an article I read a while back reviewing the performance between ipairs/pairs and an iterative loop. The site was located at http://trac.caspring.org/wiki/LuaPerformance, though that appears to be dead now. Basically, the summary is that ipairs and pairs takes about 2x as long to run as an iterative loop. If anyone knows of another source comparing the performance, that would be great to see. [import]uid: 120914 topic_id: 36857 reply_id: 145223[/import]