Forum

> > CS2D > Scripts > Insert Multiple Vars into a table
Forums overviewCS2D overview Scripts overviewLog in to reply

English Insert Multiple Vars into a table

5 replies
To the start Previous 1 Next To the start

old Insert Multiple Vars into a table

The Superman
User Off Offline

Quote
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
edited 1×, last 04.12.19 08:12:20 pm

old Re: Insert Multiple Vars into a table

Mora
User Off Offline

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

old Re: Insert Multiple Vars into a table

The Superman
User Off Offline

Quote
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
edited 1×, last 05.12.19 11:40:30 am

old Re: Insert Multiple Vars into a table

Masea
Super User Off Offline

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

old Re: Insert Multiple Vars into a table

The Superman
User Off Offline

Quote
@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)

old Re: Insert Multiple Vars into a table

Mami Tomoe
User Off Offline

Quote
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)
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview