Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Let's say I have a loop that is creating variables for every element in a list, with the script like this:

local nameList = {"Maxime", "Ivan", "Ian", "Luna", "Wes"}

for _, i, in nameList do
[insert script here]
end

What I have tried:

In the section that says [insert script here] is it possible for me to write something that takes each element of the list, and concatenates it with a second part, then names a local variable the result of that? So, something like: local (first element in nameList .. "age") = 8

Then on the second time: (Second element in nameList .. "age") = 23, and so on?
Posted
Updated 27-Aug-20 7:27am
v2

I would use a table for such a task, e.g.

Lua
local nameList = {"Maxime", "Ivan", "Ian", "Luna", "Wes"}

local var = {}

for _, v in pairs(nameList) do
  var[v .. "_age"] = math.random(1,100)
end

print(var.Ian_age)
 
Share this answer
 
No, you can't. This is what arrays are for.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900