Forum

> > CS2D > Scripts > Find out the biggest value in a table
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Find out the biggest value in a table

7 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Find out the biggest value in a table

sixpack
User Off Offline

Zitieren
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?

alt Re: Find out the biggest value in a table

Banaan
User Off Offline

Zitieren
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

alt Re: Find out the biggest value in a table

sixpack
User Off Offline

Zitieren
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?
2× editiert, zuletzt 29.12.10 21:36:03

alt Re: Find out the biggest value in a table

Banaan
User Off Offline

Zitieren
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

alt Re: Find out the biggest value in a table

Yasday
User Off Offline

Zitieren
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 hat geschrieben
1
2
3
-------------------------
-- max = { value, id }
-------------------------
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht