ID:179104
 
I was wondering if there is a built-in function in DM for finding the greatest number within a list. If not, does one of the demos/tutorials include such a proc? I would need it to accept a list which may include negative integers (it should correctly identify 2 as being greater than -3, etc.). I know it wouldn't be too terribly difficult to write my own, but this seems like a useful proc that would have been done somewhere before (perhaps even within DM).

Thanks.
I just tried many different ways of finding it, but none prevailed. But I'm certain that you should use the max() proc.

Sorry I couldn't help.

-Rcet
In response to Rcet (#1)
Yea, max() doesn't seem to work with a list as an argument (max(thelist) returns null). Is there a way to do something like:
max(thelist[1],...,thelist[thelist.len]), when you could potentially have anywhere from 1 to 50 elements in thelist?

Thanks.


Rcet wrote:
I just tried many different ways of finding it, but none prevailed. But I'm certain that you should use the max() proc.

Sorry I couldn't help.

-Rcet
In response to Rcet (#1)
Rcet wrote:
I just tried many different ways of finding it, but none prevailed. But I'm certain that you should use the max() proc.

Sorry I couldn't help.

-Rcet

I ended up just using this:

proc/Findmax(list/thelist)
var/highest = thelist[1]
for(var/i = 1, i <= thelist.len, i++)
if(thelist[i] > highest)
highest = thelist[i]
return highest

If someone knows of a shortcut (ie. with a new function in the byond beta), I'd still like the hear about it.
dramstud wrote:
I was wondering if there is a built-in function in DM for finding the greatest number within a list. If not, does one of the demos/tutorials include such a proc? I would need it to accept a list which may include negative integers (it should correctly identify 2 as being greater than -3, etc.). I know it wouldn't be too terribly difficult to write my own, but this seems like a useful proc that would have been done somewhere before (perhaps even within DM).

min() and max() should work with a list argument, so long as the list contains arguments of the same type (number, text, and so forth). This feature was introduced in one of the earlier 307 betas. If you find an example that does not work, please post the code.
In response to Tom (#4)
I already have a find max proc in my current project where it just sets the first item in the list as the maximum then loops through the list replacing it with any members that are greater than the current maximum.

Would the built in one be more efficient and make it worth changing it or does the built in feature basically work the same way?
I think this will work:
proc/returnmax(var/list/L = list())
var/max = -1e#inf
for(var/i in L)
var/number = 0
if(isnum(i)) number = 1
else if(isnum(text2num(i))) number = text2num(i)
if(number > max) max = number
return max

-Lord of Water