## [Solved] get table in-between {}

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
GVovkiv
Prole
Posts: 33
Joined: Fri Jan 15, 2021 7:29 am

### [Solved] get table in-between {}

So, i kinda new to lua and i encountered with problem:

Code: Select all

a = {a, b}
a.b = {
{
c = 1
}
}

print(a.b.c) --> nil instead of 1
so there exist way to get tables like this without deleting this brackets?:

Code: Select all

{
c = 1
}
Last edited by GVovkiv on Sat Jan 23, 2021 1:29 pm, edited 1 time in total.
zorg
Party member
Posts: 3074
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: get table in-between {}

With a = {a, b}, you're actually doing this: a = {[1] = a, [2] = b}, and at that time, both a and b are either nil, or whatever you set those, if you did.

The second issue is you doubling up the brackets, so again, you'll get this:
a.b = {[1] = {['c'] = 1}}

which can be printed with a.b[1].c instead... lose the extra brackets and it'll work:
a = {}
a.b = {
c = 1
}

You can also do it with the opening bracket on its own line, lua shouldn't complain:
a = {}
a.b =
{
c = 1
}
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
GVovkiv
Prole
Posts: 33
Joined: Fri Jan 15, 2021 7:29 am

### Re: get table in-between {}

zorg wrote: Fri Jan 22, 2021 10:31 pm With a = {a, b}, you're actually doing this: a = {[1] = a, [2] = b}, and at that time, both a and b are either nil, or whatever you set those, if you did.

The second issue is you doubling up the brackets, so again, you'll get this:
a.b = {[1] = {['c'] = 1}}

which can be printed with a.b[1].c instead... lose the extra brackets and it'll work:
a = {}
a.b = {
c = 1
}

You can also do it with the opening bracket on its own line, lua shouldn't complain:
a = {}
a.b =
{
c = 1
}
Well, a.b[1].c it exactly what I need, thank you
(but it kinda inconvenient)

Main problem with it I experience in Tiled
When it creat .lua file, it put some tables like

Code: Select all

tileset = {
{
<Data>
}
}
with double brackets instead of one
And it sucks

Manually deleting it can be painful, especially when you create/modify maps often
ohai
Prole
Posts: 20
Joined: Thu Jan 21, 2021 9:06 pm

### Re: get table in-between {}

GVovkiv wrote: Sat Jan 23, 2021 12:45 am Main problem with it I experience in Tiled
When it creat .lua file, it put some tables like

Code: Select all

tileset = {
{
<Data>
}
}
with double brackets instead of one
And it sucks
It's for a reason - Tiled has to have a way to denote multiple tilesets. Your quick snippet actually says "tileset" instead of "tilesets", so I guess that's where the confusion came from. If you only use one tileset, then it's absolutely correct to just take the first element from the table using [1]. I guess that for some use cases it would be better if Tiled exported it like:

Code: Select all

tilesets = {
ground = { <Data> }
trees = { <Data> }
}

but I guess they didn't put that much thought into it. Or maybe I'm wrong and it's more important for them to have tilesets ordered and be able to use ipairs() instead of pairs() to iterate over.
GVovkiv
Prole
Posts: 33
Joined: Fri Jan 15, 2021 7:29 am

### Re: get table in-between {}

ohai wrote: Sat Jan 23, 2021 9:47 am
GVovkiv wrote: Sat Jan 23, 2021 12:45 am Main problem with it I experience in Tiled
When it creat .lua file, it put some tables like

Code: Select all

tileset = {
{
<Data>
}
}
with double brackets instead of one
And it sucks
It's for a reason - Tiled has to have a way to denote multiple tilesets. Your quick snippet actually says "tileset" instead of "tilesets", so I guess that's where the confusion came from. If you only use one tileset, then it's absolutely correct to just take the first element from the table using [1]. I guess that for some use cases it would be better if Tiled exported it like:

Code: Select all

tilesets = {
ground = { <Data> }
trees = { <Data> }
}

but I guess they didn't put that much thought into it. Or maybe I'm wrong and it's more important for them to have tilesets ordered and be able to use ipairs() instead of pairs() to iterate over.
("tileset" just for example)

Well, thanks for explaining, but i found out it myself

for example, in tiled i can create 2 layer with different objects on it
in exported file it will looks something like

Code: Select all

layers = {
{ -- first layer
*layer 1 objects*
}

{ -- 2nd layer
*layer 2 objects*
}
}

which, i believe, can be fetch with something like:

Code: Select all

l = 1 -- layer index, i guess
print(layers[l])

where "l" can be "l = l + 1" when game done loading layer 1 or sometning like that
darkfrei
Party member
Posts: 173
Joined: Sat Feb 08, 2020 11:09 pm

### Re: [Solved] get table in-between {}

GVovkiv wrote: Fri Jan 22, 2021 10:21 pm So, i kinda new to lua and i encountered with problem:

Code: Select all

a = {a, b}
a.b = {
{
c = 1
}
}

print(a.b.c) --> nil instead of 1
so there exist way to get tables like this without deleting this brackets?:

Code: Select all

{
c = 1
}

Code: Select all

print(a.b[1].c)
GVovkiv
Prole
Posts: 33
Joined: Fri Jan 15, 2021 7:29 am

### Re: [Solved] get table in-between {}

darkfrei wrote: Sat Jan 23, 2021 7:49 pm
GVovkiv wrote: Fri Jan 22, 2021 10:21 pm So, i kinda new to lua and i encountered with problem:

Code: Select all

a = {a, b}
a.b = {
{
c = 1
}
}

print(a.b.c) --> nil instead of 1
so there exist way to get tables like this without deleting this brackets?:

Code: Select all

{
c = 1
}

Code: Select all

print(a.b[1].c)
Question was already solved, but thanks for answer anyway

### Who is online

Users browsing this forum: Google [Bot] and 27 guests