Forum

> > CS2D > Scripts > Table removing value or nil this value in loop
Forums overviewCS2D overview Scripts overviewLog in to reply

English Table removing value or nil this value in loop

17 replies
To the start Previous 1 Next To the start

old Table removing value or nil this value in loop

Mora
User Off Offline

Quote
Hello FunkyFarts , okno.
uss.de.

I have problem with removing keys from a table.
I understand what's not working and why, but i can't find solution for that:
Let's think there is a table
1
2
3
4
5
6
T={
[1]={},
[2]={},
[3]={}}

T[2]=nil
if i use loop and remove some [x] keys it would work perfectly, but leave missing ids, such as
1
2
3
T={
[1]={},
[3]={}}

If i use
table.remove(t,pos)
it lead to loop working not correct, you know why.

Well, the first method would be great but how to make after that loop that table is fixed with this example:
1
2
3
4
5
6
7
8
9
table={
	[1]="one",
	[2]="two",
	[3]="three",
	[4]="four",
	[5]="five"}
table[1]=nil
table[3]=nil
table[4]=nil
table will be
1
2
3
table={
	[2]="one",
	[5]="five}
so i want 2 to be 1, 5 to be 2. And anytime i have this problem it would i fix it.
Any ideas?

old Re: Table removing value or nil this value in loop

Talented Doge
User Off Offline

Quote
1
2
3
4
5
6
7
8
t = {
[1] = "k",
[2] = "f",
[3] = "p"
}

table.remove(t, 2)
print(t[2])

I have tested it and it returned p as the result.

More specifically, from which table and what element are you trying to remove? Player list?

old Re: Table removing value or nil this value in loop

Yates
Reviewer Off Offline

Quote
user Mora has written
1
key=nil


If you're doing this, it won't work. Key and value are just temporary block scope variables for the for loop.

Replace it with this instead:
1
inv[key] = nil

But honestly I still don't get why
table.remove
isn't working for you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
aVeryCoolTable = {
    [1] = "a",
    [2] = "b",
    [3] = "c",
    [4] = "d",
}

print(aVeryCoolTable[2]) -- print b
table.remove(aVeryCoolTable, 2) -- remove b
print(aVeryCoolTable[2]) -- print c

for key, value in ipairs(aVeryCoolTable) do
    if key == 2 then
        table.remove(aVeryCoolTable, 2) -- remove c
        print(aVeryCoolTable[2]) -- print d
    end
end

This all works like it should, test it at https://www.lua.org/cgi-bin/demo

old Re: Table removing value or nil this value in loop

Mora
User Off Offline

Quote
Well, i'm already using for inv[key] and it's work perfectly, but you should know: while in LOOP you remove an item it lead to shit. Because the loop is not starting from 1-st key after removed item, but continue.
There is screen of inv[key]=nil
IMG:https://i.imgur.com/RLW2sUG.png

IMG:https://i.imgur.com/8uFIDXs.png

IMG:https://i.imgur.com/ijb0JRT.png

old Re: Table removing value or nil this value in loop

Yates
Reviewer Off Offline

Quote
@user Mora: Use two loops and use table.remove or save the keys in a separate table and remove things afterwards (resulting in three loops eh).

Your approach is clearly wrong, I'm afraid if you cannot solve it we'll need to look at some code.

old Re: Table removing value or nil this value in loop

Mora
User Off Offline

Quote
Nah, that's what im doing, i'm not using mine inventory, i'm using copy of it and work with it.
i'm using two loops:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function MORA_MOD.cooking(id,slot)
	local inv=table.copy(PLAYERS[id].INVENTORY)
	local info={}
	for k, v in pairs(ITEMS[PLAYERS[id].RECIPES[slot]].REQ) do
	table.insert(info,{k=false})
	print(v[1],v[2])
		for key, value in pairs(inv) do
			if v[1]==value.ID then
				if value.STACK then
					if value.STACK>v[2] then
						value.STACK=value.STACK-v[2]
						info[k].k=true
						msg2(id,"-x")
					elseif value.STACK==v[2] then
						inv[key]=nil
						info[k].k=true
						msg2(id,"-1")
					else
						msg2(id,COLOUR[2].."[Cooking]: You dont have enough items of this component["..ITEMS[v[1]].NAME.."].")
						break
					end
				else
					inv[key]=nil
					info[k].k=true
				end
			end
		end
	end
then i do
PLAYERS[id].INVENTORY=inv
if i have all required items. And it's working but missing keys id.
IMG:https://i.imgur.com/heEEybl.png

old Re: Table removing value or nil this value in loop

Yates
Reviewer Off Offline

Quote
You can solve this by using a table and saving the keys you want to remove, then after the loop is complete you can loop over it to remove them then set your
PLAYERS[id].INVENTORY
table.

Basically: You should check if the player has the items first, then remove them.

old Re: Table removing value or nil this value in loop

Talented Doge
User Off Offline

Quote
This scenario can be done by this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
x = {}

t = {
[1] = {"a", 1},
[2] = {"b", 3},
[3] = {"c", 4}
}

c = {2,3,5}

for k, v in ipairs(t) do
	if c[k] == v[2] then
		print("IM INSERTING "..v[1])
		table.insert(x,v[1])
	end
end

for i = 1, #x do
	for k = 1, #t do
		if t[k][1] == x[i] then table.remove(t, k) break end
	end
end

for i = 1, #t do
	print("THIS IS "..t[i][1])
end

old Re: Table removing value or nil this value in loop

Mora
User Off Offline

Quote
Lel, i didn't know that, but it's almost identical like i said in previous post.
Anyway i prefer using yours if it would work.
Thank you for your help: user Yates, user Talented Doge!
!!
Yates, bcs i'm not just removing 1 item.. if you read my post you will see that i'm using LOOP inside loop:
IMG:https://i.imgur.com/D6FbzqX.png

This is silly to make table if you have 1 item in it right?
If i'd use 1 item i would not have problems
1
2
3
REQ={
{6,2},
{7,2}}
edited 1×, last 31.07.17 12:45:13 pm

old Re: Table removing value or nil this value in loop

Mora
User Off Offline

Quote
Oh ye, i forget, i just clicked reply and prepared mine message, but site notice me i need to edit my reply, so i lost it and replied to your message.

I just did
PLAYERS[id].INVENTORY=inv
instead of
PLAYERS[id].INVENTORY=table.copy(inv)

Because i have stackable items and if i found this item in my inventory i'll add to .STACK value +1

Excuse me, my bad. Now anything is almost completely fixed.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview