Neil Penning
Neil Penning

Reputation: 13

How to add a value to an embeded list in lua

I have three lists within a list, and I was wondering how to add a value to the last embedded list. Here is an example of the list that I have:

l = 
{{1, 2, 3},
{4, 5, 6},
{7, 8}}

If I wanted to add the value 9 to the last embedded list, I would do

l[#l][#l[#l] + 1] = 9

which would change l to

{{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}

Is there an easier way to do this?

Upvotes: 1

Views: 137

Answers (1)

siffiejoe
siffiejoe

Reputation: 4271

No.

But you can make it easier to read by using a temporary variable:

local last = l[ #l ]
last[ #last+1 ] = 9

There is also a table.insert function which appends to the end of a sequence by default:

table.insert( l[ #l ], 9 )

Upvotes: 4

Related Questions