English Insert Multiple Vars into a table

5 replies
Goto Page
To the start Previous 1 Next To the start
Up
The Superman
User
Offline Off
Hello guys, How to insert multiple variables into a table in lua?

I have written this:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
local OBS = { }
local Q = 20
local W = 1

InsertMultipleVars = function (var, ...)

    for k, v in pairs({...}) do
        v = var[k]
    end
    
end

InsertMultipleVars(OBS, Q, W)

print(OBS[Q]) -- Returns nil


But it's not working
edited 1×, last 04.12.19 08:12:20 pm
04.12.19 03:13:28 pm
Up
Mora_rus
User
Offline Off
Code:
1
2
3
4
5
6
7
8
9
InsertMultipleVars = function (var, ...)

    for k, v in pairs({...}) do
        table.insert(var,k)
    end
    
end

InsertMultipleVars(OBS,Q, W)

print(OBS[Q]) -- Returns 2

print(OBS[W]) -- Returns 1

idk if it really what u wanted but it works for me now.
04.12.19 06:12:38 pm
Up
The Superman
User
Offline Off
Oh yeah, but it returns nil if the number is more than 1-digit.

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
local OBS = { }
local Q = 2000
local W = 1900

InsertMultipleVars = function (var, ...)

    for k, v in pairs({...}) do
        table.insert(var, k)
    end
    
end

InsertMultipleVars(OBS, Q, W)
print(OBS[Q]) -- nil
print(OBS[W]) -- nil
edited 1×, last 05.12.19 11:40:30 am
04.12.19 08:07:54 pm
Up
Masea
Super User
Offline Off
@user The Superman: That's because you are not specifying the keys but only the values. Print
OBS[1]
in the same scenario once again and see what it returns. And of course,
InsertMultipleVars(OBS, Q, W)
won't work because there isn't such a variable. Use
"Q"
&
"W"
instead.
Create your UI easy and fast: UI Framework | Go deeper into the darkness and test your bravery: file cs2d Outlast 2 Modification (27)
05.12.19 11:45:11 am
Up
The Superman
User
Offline Off
@user Masea: Alright, I got it. Thanks guys

But can anyone tell me which method is faster? (to avoid lags)

Method #1
Code:
1
2
3
4
5
6
7
8
9
InsertMultipleVars = function (var, ...)

    for k, v in pairs({...}) do
        table.insert(var, k)
    end
    
end

InsertMultipleVars(OBS, Q, W)

Or

Method #2
Code:
1
2
3
4
5
6
7
8
9
InsertMultipleVars = function (var, ...)

    for k, v in pairs({...}) do
        v = var[k]
    end
    
end

InsertMultipleVars(OBS, Q, W)
05.12.19 12:20:46 pm
Up
Goo
User
Offline Off
This:

Code:
1
2
3
4
5
6
7
8
9
local function InsertMultipleVars(var, ...)

    for k, v in pairs({...}) do
        v = var[k]
    end
    
end

InsertMultipleVars(OBS, Q, W)


If you plan to use it in other Lua files you may do this:

-- Use this for local access, feel free to rename but update the function below it.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
local function _InsertMultipleVars(var, ...)

    for k, v in pairs({...}) do
        v = var[k]
    end
    
end

-- Use this for global access
function InsertMultipleVars(...)
     return _InsertMultipleVars(...)
end

_InsertMultipleVars(OBS, Q, W)
To the start Previous 1 Next To the start