I have had a Min/Max function in my library for years that used a variadic parameter so that you could pass it as many parameters as you liked. This allowed it to also work with a simple array parameter if followed by a * but I discovered today that it did not work for associative arrays so I made a small tweak.

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus

Min(X:="�", List*) ; X := chr(0xffff)

{

for key, element in List

if (element < X)

X := element

return X

}

Max(X:="", List*)

{

for key, element in List

if (element > X)

X := element

return X

}

The Min function requires Unicode encoding of the file as it uses a special extended character.

If that is a problem this ANSI encoding function can be used.

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

Min(List*)

{

List._NewEnum().Next(,X)

for key, element in List

if (element < X)

X := element

return X

}

`_NewEnum().Next`

to get the starting value for comparison in an associative array which makes this function slower than the one above.Examples of usage:

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus

; Basic Usage

MsgBox % Min(2,-11,4,3) ; -11

SimpleArrayofNumbers := [2,-11,4,3]

MsgBox % Min(SimpleArrayofNumbers*) ; -11

SimpleArrayofText := ["cherry","apple","pear","banana"]

MsgBox % Min(SimpleArrayofText*) ; apple

SimpleArrayofMixed := [2,1,"Tree","Pear",3]

MsgBox % Max(SimpleArrayofMixed*) ; Tree

AssociativeArray := {"one":"cherry","two":"apple","three":"pear","four":"banana"}

MsgBox % Max(AssociativeArray*) ; pear

Even thought Min/Max functions are fairly simple I figured these were worth posting as the ability to find the Min or Max value in an array is a pretty common need. These are also the fastest functions I could find that handled all the cases these functions can handle.

There can be 1 or more parameters that can be literals, variables, simple arrays, or associative arrays.

FG