Forum

> > CS2D > Scripts > Insert Multiple Vars into a table
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Insert Multiple Vars into a table

5 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Insert Multiple Vars into a table

The Superman
User Off Offline

Zitieren
Hello guys, How to insert multiple variables into a table in lua?

I have written this:
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
1× editiert, zuletzt 04.12.19 20:12:20

alt Re: Insert Multiple Vars into a table

Mora
User Off Offline

Zitieren
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.

alt Re: Insert Multiple Vars into a table

The Superman
User Off Offline

Zitieren
Oh yeah, but it returns nil if the number is more than 1-digit.

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
1× editiert, zuletzt 05.12.19 11:40:30

alt Re: Insert Multiple Vars into a table

Masea
Super User Off Offline

Zitieren
@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.

alt Re: Insert Multiple Vars into a table

The Superman
User Off Offline

Zitieren
@user Masea: Alright, I got it. Thanks guys

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

Method #1
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
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)

alt Re: Insert Multiple Vars into a table

Mami Tomoe
User Off Offline

Zitieren
This:

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.
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)
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht