'Using pair/ipair to simplify a lua function
I have the following code that works:
function group1color()
for _, roomid in ipairs(group1rooms) do
if roomid == '1+' then
Controls.room1background.Color = '#dba643'
end
if roomid == '2+' then
Controls.room2background.Color = '#dba643'
end
if roomid == '3+' then
Controls.room3background.Color = '#dba643'
end
end
end
However, I need to copy the roomid if statement 5 more times, and then copy the function 8 times for eight different room groups. I am trying to figure out if there is a way to simplify this using pairs or ipairs? I am mainly looking for suggestions on how to simplify the iterations in the function.
Solution 1:[1]
You can access global variable by _G["varname"]
function groupcolor()
for N = 1, 8 do
local groupNrooms = _G["group"..N.."rooms"]
for _, roomid in ipairs(groupNrooms) do
local M = roomid:match"^(%d+)%+$"
if M then
Controls["room"..M.."background"].Color = '#dba643'
end
end
end
end
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Egor Skriptunoff |
