Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve another variable within table in Lua?

how can I use title in shortdesc?

This fails:

function descriptor()
    return {
        title = "This";
        shortdesc = title .. " is my text."
    }
end
like image 394
cy221 Avatar asked Sep 22 '26 15:09

cy221


2 Answers

You can't; you can use a local variable though:

function descriptor()
    local title = "This"
    return {
        title = title;
        shortdesc = title .. " is my text."
    }
end
like image 78
Paul Kulchenko Avatar answered Sep 24 '26 05:09

Paul Kulchenko


Paul is correct. You can also construct the table in pieces:

function descriptor()
    local t = {}
    t.title = "This";
    t.shortdesc = t.title .. " is my text."
    return t
end
like image 41
Doug Currie Avatar answered Sep 24 '26 07:09

Doug Currie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!