Forum

> > CS2D > Scripts > table insertion...
Forums overviewCS2D overview Scripts overviewLog in to reply

English table insertion...

6 replies
To the start Previous 1 Next To the start

old table insertion...

omg
User Off Offline

Quote
how would i go about inserting a table into another table like this?
1
2
3
4
amazing={badvariable=0,worsevariable="banana bonanza"}
incredible={}

incredible:insert(amazing)
the problem is, i want to be able to change badvariable to some non-default number, etc

old Re: table insertion...

Flacko
User Off Offline

Quote
I don't get what you mean. Are you talking about the colon table:pseudomember() syntax?

old Re: table insertion...

Gajos
BANNED Off Offline

Quote
I didn't tested it
1
2
3
4
5
6
7
8
9
amazing={
	badvariable = 0;
	worsevariable = "banana bonanza";
}
incredible={} 

for _, i in pairs(amazing) do
	table.insert(incredible,amazing[i])
end

old Re: table insertion...

omg
User Off Offline

Quote
i mean i want to insert an amazing into incredible, but change the key values of amazing so that theyre not the initialized values

old Re: table insertion...

EngiN33R
Moderator Off Offline

Quote
Uh.

1
2
3
4
5
6
7
8
9
10
11
12
table.refinsert = function(tbl,tbl2,ref)
	for k,v in pairs(tbl2) do
		tbl[ref[k]]=v
	end
end

amazing={badvariable=0,worsevariable="banana bonanza"}
incredible={}

referencetable = {badvariable=5,worsevariable=16} -- move badvariable to index 5, worsevariable - to index 16

table.refinsert(incredible,amazing,referencetable)

Is this what you want? I think this should work.

old Re: table insertion...

Flacko
User Off Offline

Quote
I think you actually meant to change the mapped values of amazing. The key values are the ones used between brackets to access their mapped ones.
This is an attempt to a practical example code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
languages = {
	new = function(t, base, init)
		local r = {}
		for k,v in pairs(base) do
			--copy the table taking either the default value or overwrite if possible
			r[k] = init[k] or v
		end
		table.insert(t,r)
		return r
	end
}
--no third variable needed
english = languages:new({hello="hello", bye="bye", yes="yes", no="no"})
--no 'no' key needed, because 'no' in spanish is written just like in english
spanish = languages:new(english, {hello="hola", bye="adios", yes="si"})

old Re: table insertion...

omg
User Off Offline

Quote
oh right the mapped values, yeah
well...i ended up figuring it out. what i was looking for was something like this
1
2
3
4
5
6
7
8
9
10
11
12
amazing={blah=0,blah2=""}
incredible={}
...
	local r=table.copy(amazing)
	r[blah]=notzero
	r[blah2]=notempty
	or
	for k,v in pairs(newvalues) do
		r[k]=v
	end
	incredible:insert(r)
...
edited 2×, last 13.02.13 01:16:43 am
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview