Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Table as parameter in lua

I want to have a table named contents as a parameter of a function named func.
So when I call this function I can input strings into the table at the instance the function is called as follows:

function func(contents)
  
  contents = {}
  print(table.concat(contents, ' '))

end

func({'content1', 'content2', 'content3'})

NOTE : the table should be created inside the function’s parameter

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

What you are asking for is the same as simply using table.concat() directly itself, there is no need to define a function for it unless you want to add additional manipulation.

function func(contents)
    return table.concat(contents, " ")
end

func({"content1", "content2", "content3"}) -- This returns 'content1 content2 content3'.

The above is the same as:

table.concat({"content1", "content2", "content3"}, " ") -- Returns 'content1 content2 content3'.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading