attempt to call global 'horses' (a nil value)

I tried to create a table and I got this error:

attempt to call global ‘horses’ (a nil value)

Here is all the code I have:

horses{1,2,3,4,5} horses[1].name="Gary" horses[1].age=45 horses[2].name="Barry" horses[2].age=86 horses[3].name="Harry" horses[3].age=420 horses[4].name="Larry" horses[4].age=5 horses[5].name="Jerry" horses[5].age=23

What is wrong with it?

That isn’t how tables work.

Rather, you’re not making a table of tables with this statement:

horses{1,2,3,4,5}

If you want a table of tables do this:

horses = {{},{},{},{},{}}

or much better, write your code like this:

local horses = {} local horse = {} horse.name="Gary" horse.age=45 horses[1] = horse local horse = {} horse.name="Barry" horse.age=86 horses[2] = horse ...

Better take a time out and read the first half of the PIL:

https://www.lua.org/pil/

Tables are talked about here:

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

I tried to create a table and I got this error:

attempt to call global ‘horses’ (a nil value)

Here is all the code I have:

horses = {} <-- this is the fix horses[1].name=“Gary” horses[1].age=45 horses[2].name=“Barry” horses[2].age=86 horses[3].name=“Harry” horses[3].age=420 horses[4].name=“Larry” horses[4].age=5 horses[5].name=“Jerry” horses[5].age=23

What is wrong with it?

@sgs,

He still needs to insert the tables first or indexing into the parent table for sub-tables will fail.

i.e.

horses = {} -- sgs horses[1] = {} -- +this horses[1].name="Gary" horses[1].age=45 horses[2] = {} -- +this horses[2].name="Barry" horses[2].age=86 ..

Agreed and good call… but I thought that was implied?

I was (my bad) not explicit enough.

That isn’t how tables work.

Rather, you’re not making a table of tables with this statement:

horses{1,2,3,4,5}

If you want a table of tables do this:

horses = {{},{},{},{},{}}

or much better, write your code like this:

local horses = {} local horse = {} horse.name="Gary" horse.age=45 horses[1] = horse local horse = {} horse.name="Barry" horse.age=86 horses[2] = horse ...

Better take a time out and read the first half of the PIL:

https://www.lua.org/pil/

Tables are talked about here:

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

I tried to create a table and I got this error:

attempt to call global ‘horses’ (a nil value)

Here is all the code I have:

horses = {} <-- this is the fix horses[1].name=“Gary” horses[1].age=45 horses[2].name=“Barry” horses[2].age=86 horses[3].name=“Harry” horses[3].age=420 horses[4].name=“Larry” horses[4].age=5 horses[5].name=“Jerry” horses[5].age=23

What is wrong with it?

@sgs,

He still needs to insert the tables first or indexing into the parent table for sub-tables will fail.

i.e.

horses = {} -- sgs horses[1] = {} -- +this horses[1].name="Gary" horses[1].age=45 horses[2] = {} -- +this horses[2].name="Barry" horses[2].age=86 ..

Agreed and good call… but I thought that was implied?

I was (my bad) not explicit enough.