Forum

> > CS2D > Scripts > Find out the biggest value in a table
Forums overviewCS2D overview Scripts overviewLog in to reply

English Find out the biggest value in a table

7 replies
To the start Previous 1 Next To the start

old Find out the biggest value in a table

sixpack
User Off Offline

Quote
Hi!
I got a variable:
score = {}
score[id] = 1
etc.

How to discover the biggest value of them, get the player's id who has this value and of course the value itself?

old Re: Find out the biggest value in a table

Banaan
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
-------------------------
-- max = { value, id }
-------------------------

max = {0}

for i,v in ipairs(score) do
	if(v>max[1]) then max={v,i} end
end

old Re: Find out the biggest value in a table

sixpack
User Off Offline

Quote
What I actually want to do is to find the #1 Ranked CT and the #1 Ranked T from the TAB menu in each end of a round... Anyone can help me with this?
edited 2×, last 29.12.10 09:36:03 pm

old Re: Find out the biggest value in a table

Banaan
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
-------------------------
-- max = { value, id }
-------------------------

addhook("endround","findmax")

function findmax()

maxt = {0}
maxct = {}

for i,v in ipairs(player(0,"team1")) do
	if(player(v,"score")>maxt[1]) then maxt={player(v,"score"),v} end
end

for i,v in ipairs(player(0,"team1")) do
	if(player(v,"score")>maxct[1]) then maxct={player(v,"score"),v} end
end

end

old Re: Find out the biggest value in a table

Yasday
User Off Offline

Quote
Try this function, it returns the largest numeric value in a table:
1
2
3
4
5
6
7
function table.max(tbl)
	local tbl2 = {}
	for i,v in pairs(tbl) do
		tbl2[v] = i
	end
	return table.maxn(tbl2)
end
Or just use Banaan's script.
Edit:
@DaKnOb
Banaan has written
1
2
3
-------------------------
-- max = { value, id }
-------------------------
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview