Forum

> > CS2D > Scripts > table keys
Forums overviewCS2D overview Scripts overviewLog in to reply

English table keys

14 replies
To the start Previous 1 Next To the start

old table keys

omg
User Off Offline

Quote
is it possible to copy a table by doing something like this?
1
2
3
4
5
tbl={lol=1,lmao=0,rofl=1}
newtbl={lol=0,lmao=0,rofl=0}
for key,value in pairs(tbl) do
	newtbl.key=value
end
it may look strange and inefficient, but i have to copy this way because its actually part of a file io using tables

old Re: table keys

Avo
User Off Offline

Quote
If you mean just copy table there are to diffrent ways:

1. Copy adress to table (if you change value in tbl, value in newtbl will be changed, too)
1
newtbl=tbl

2. Use function from my private library:
1
2
3
4
5
6
7
8
function CopyTable(t)
	local copy={}
	for i=1,#t do
		copy[i]=t[i]
	end
	return copy
end
newtbl=CopyTable(tbl)

old Re: table keys

Flacko
User Off Offline

Quote
Yes, it's possible to copy it that way. Although you have a bug there.
newtbl.key equals to newtbl["key"].
The correct script should be
1
2
3
for key,value in pairs(tbl) do
	newtbl[key] = value
end

If you want a 'deep copy' that also duplicates all the tables inside the table it's enough with just calling the function recursively.
1
2
3
4
5
6
7
8
9
10
11
function table.copy(t)
	local r = {}
	for k,v in pairs(t) do
		if type(v) == "table" then
			r[k] = table.copy(v)
		else
			r[k] = v
		end
	end
	return r
end

old Re: table keys

omg
User Off Offline

Quote
wrong idea, im using keys to keep track of the values. ur method uses indexes, doesnt it? or am i not aware of something?

old Re: table keys

EngiN33R
Moderator Off Offline

Quote
user omg has written
wrong idea, im using keys to keep track of the values. ur method uses indexes, doesnt it? or am i not aware of something?


I might be mistaken, but that confusion is caused by different terminology. An index of a table has a key and a value. Key is the position in the table.

Flacko's code is, essentially, the copy function. You can have a deep copy, too:

1
2
3
4
5
6
7
8
9
10
11
12
function table.deepcopy(t)
     local r = {}
     for k,v in pairs(t) do
          if type(v) == "table" then
               r[k] = table.copy(v)
          else
               r[k] = v
          end
     end
     setmetatable(r,getmetatable(t))
     return r
end

old Re: table keys

Starkkz
Moderator Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function table.copy(a,b)
	for k, v in pairs(a) do
		if type(v) == "table" then
			if not b[k] or (b[k] and type(b[k]) ~= "table") then
				b[k] = table.clone(v)
			elseif b[k] and type(b[k]) == "table" then
				table.copy(v, b[k])
			end
		elseif type(v) == "function" then
			b[k] = loadstring(string.dump(v))
		else
			b[k] = v
		end
	end
	return b
end

function table.clone(t)
	return table.copy(t, {})
end

So you use it this way, all the elements of the first table will be copied to the second one.

1
2
3
4
5
6
myTab = {asd = 123}
secondTab = {}

table.copy(myTab, secondTab)

print(secondTab.asd)
edited 2×, last 14.07.12 10:00:45 pm

old Re: table keys

Flacko
User Off Offline

Quote
user omg has written
i got it, flacko was right all along lol

Actually you were right all along, except for that small bug I mentioned.

old Re: table keys

Apache uwu
User Off Offline

Quote
Wait.

Are you copying a table 100%?

Couldn't you just do this?

1
2
tbl={lol=1,lmao=0,rofl=1}
newtbl=tbl

That would make it so newtbl is identical to tbl.

However, it's probably not what you wanted.

Unless you want to merge the tables, well in that case you could just use this.

1
2
3
4
5
6
7
8
9
10
11
12
function merge(t1,t2,bool)
	for k, v in pairs(t2) do
		if type(v)=="table" and type(t1[k] or false)=="table" then
			merge(t1[k],t2[k])
		else
			if not bool or bool and t1[k]==nil then
				t1[k]=v
			end
		end
	end
	return t1
end

So.

1
2
3
4
5
6
7
8
9
10
a={
	["index_one"]={"epic"},
	["index_2"]=12,
	"4"=true
}
b={
	["xD"]={":("},
	["lol"]="ala",
	"4"=false
}

Now.

1
2
3
4
5
6
7
8
9
10
11
c=merge(a,b,false)
	--c now equals
	--[[
	{
		["index_one"]={"epic"},
		["index_2"]=12,
		["xD"]={":("},
		["lol"]="ala",
		4=true
	}
	--]]

In this example, merge's third parameter is false (default), which means it will not replacing already existing keys in the first table.

However by using:

c=merge(a,b,true)


c[4] would equal false!

old Re: table keys

EngiN33R
Moderator Off Offline

Quote
user Apache uwu has written
Wait.

Are you copying a table 100%?

Couldn't you just do this?

1
2
tbl={lol=1,lmao=0,rofl=1}
newtbl=tbl

That would make it so newtbl is identical to tbl.


It would never do that - if you do what you did in that piece of code, newtbl will be a kind of a 'pointer' to tbl, and all changes made to newtbl will be reflected onto tbl, and vice versa.

old Re: table keys

Apache uwu
User Off Offline

Quote
Sorry, don't fully understand, are you saying that:

1
2
3
4
5
a={
		1="a",
		["s"]="d"
	}
	b=a

If I change b, a is still the same.

old Re: table keys

EngiN33R
Moderator Off Offline

Quote
1
2
3
4
5
6
7
8
a = {1,2,3}
b = a

print(a[1]) -> 1

b[1]=2
print(b[1]) -> 2
print(a[1]) -> 2

This is what I'm saying.

old Re: table keys

Apache uwu
User Off Offline

Quote
Oh yeah tables copy by reference, try this as well.

http://lua-users.org/wiki/CopyTable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function deepcopy(object)
	local lookup_table = {}
	local function _copy(object)
		if type(object) ~= "table" then
			return object
		elseif lookup_table[object] then
			return lookup_table[object]
		end
		local new_table = {}
		lookup_table[object] = new_table
		for index, value in pairs(object) do
			new_table[_copy(index)] = _copy(value)
		end
		return setmetatable(new_table, getmetatable(object))
	end
	return _copy(object)
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview