AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Min, Max of variable number of arguments, Sorting arrays

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Laszlo



Joined: 14 Feb 2005
Posts: 4078
Location: Pittsburgh

PostPosted: Thu Mar 09, 2006 5:50 pm    Post subject: Min, Max of variable number of arguments, Sorting arrays Reply with quote

With the help of optional parameters, it is easy to write functions Min and Max, which take an arbitrary number of parameters, like Min(1,x,-1.41,y*3). The example below is limited to 10 arguments, but in the function definitions the parameter lists can be continued arbitrarily.
Code:
MsgBox % Min(1) "`n" Min(1,2) "`n" Min(9,5) "`n" Min(-1,1,-2,2,-3,3) "`n" Max(-1,1,-2,2,-3,3)

Min(x,x1="",x2="",x3="",x4="",x5="",x6="",x7="",x8="",x9="") {
   Loop
      IfEqual x%A_Index%,,Break
      Else If (x  > x%A_Index%)
               x := x%A_Index%
   Return x
}
Max(x,x1="",x2="",x3="",x4="",x5="",x6="",x7="",x8="",x9="") {
   Loop
      IfEqual x%A_Index%,,Break
      Else If (x  < x%A_Index%)
               x := x%A_Index%
   Return x
}

Handling arrays is only a little more difficult. If we pass the names to a function and the first and last index we want to process, with a handful lines of AHK code the array can be sorted, and then the first entry (Array1) is the minimum, the last entry (Array%Array0%) is the maximum, but we can also see the second largest, the median, etc. The sort is done by the built in QuickSort algorithm, so it is very fast even for huge arrays.
Code:
x0 = 10              ; Array length
Loop %x0%
   Random x%A_Index%, 1, 10
ArraySort("s","x")   ; s1, s2… <-- SORTED(x), s0 = length
Loop %x0%
   y := y s%A_Index% "`n"
MsgBox %y%

ArraySort(B, A, i0="", i1="") {     ; Sort array A%i0%...A%i1% --> B
   Local x, j                       ; Makes %B% global
   IfEqual i0,, SetEnv i0, 1        ; Default from A1...
   IfEqual i1,, SetEnv i1, % %A%0   ; Default: A0 tells the length
   IfEqual i1,, SetEnv i1, %i0%     ; If A0 is not set, either
   x := %A%%i0%
   Loop % i1-i0
   {
      j := A_Index + i0
      x := x "`n" %A%%j%
   }
   Sort x, N
   StringSplit %B%, x, `n
}
We pass the names of the arrays to the function, so the output array can only be made global implicitly (declaring locals).

Another example is with an array x of 30 random elements. Choose i0 = 8, i1 = 10, that is, sort only x8, x9 and x10. The sorted array is in s, with s0 = length, s1 the smallest element and s%s0% the largest. Now, i1-i0+1 = 3 elements are sorted, therefore s0 = 3 and s1, s2 and s3 are assigned the appropriate elements of x. If there were other elements of s already defined, like s5, their values are not changed.
Code:
s5 = -9999
Loop 30
   Random x%A_Index%, 10, 99 ; Assign some values to array x
ArraySort("s","x",8,10) ; Sort only 3 elements of x
MsgBox %s0%`n%s1%`n%s2%`n%s3%`n%s4%`n%s5%


Edit 20060309: Example call added to further explain usage


Last edited by Laszlo on Thu Mar 09, 2006 10:59 pm; edited 1 time in total
Back to top
View user's profile Send private message
Invalid User



Joined: 14 Feb 2005
Posts: 442
Location: Texas, Usa

PostPosted: Thu Mar 09, 2006 9:16 pm    Post subject: Reply with quote

When x is build, does it need empty value (ex:1|3|6|||||||) causing global array %B% to have empty elements?
_________________
my lame sig Smile
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
Laszlo



Joined: 14 Feb 2005
Posts: 4078
Location: Pittsburgh

PostPosted: Thu Mar 09, 2006 9:49 pm    Post subject: Reply with quote

If the input array has empty elements, or non-numeric values, unexpected things could happen. Similarly, if the index range given to the ArraySort function is too large. The array elements, which were not assigned a value, appear as empty strings. If x0 does not contain the number of array elements, the third argument must not be omitted. Otherwise, the local x variable does not have empty values, it is assigned all the elements of the input array, separated by new line characters.
Back to top
View user's profile Send private message
Invalid User



Joined: 14 Feb 2005
Posts: 442
Location: Texas, Usa

PostPosted: Thu Mar 09, 2006 10:11 pm    Post subject: Reply with quote

I still dont think there should be empty elements with the new array, I used
Code:

ArraySort("s","x","8","10")

To test, which 8 and 10 are in the range of x0
_________________
my lame sig Smile
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
Laszlo



Joined: 14 Feb 2005
Posts: 4078
Location: Pittsburgh

PostPosted: Thu Mar 09, 2006 10:23 pm    Post subject: Reply with quote

What do you want to do? Sort the elements x8, x9 and x10? Then use
Code:
Loop 19
   Random x%A_Index%, 10, 99
ArraySort("s","x",8,10)
Loop %s0%
   y := y s%A_Index% "`n"
MsgBox %y%
Or, do you want to find the 8th, 9th and 10th largest element in the array x?
Code:
Loop 30
   Random x%A_Index%, 10, 99
ArraySort("s","x",1,30)
MsgBox %s8%`n%s9%`n%s10%
Back to top
View user's profile Send private message
Invalid User



Joined: 14 Feb 2005
Posts: 442
Location: Texas, Usa

PostPosted: Thu Mar 09, 2006 10:34 pm    Post subject: Reply with quote

I want sort elements i0 - i1
_________________
my lame sig Smile
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
Laszlo



Joined: 14 Feb 2005
Posts: 4078
Location: Pittsburgh

PostPosted: Thu Mar 09, 2006 10:40 pm    Post subject: Reply with quote

This is the first case above, with i0 = 8, i1 = 10. The sorted array is "s", with s0 = length, s1 the smallest element and s%s0% the largest. In this example, i1-i0+1 = 3 elements are sorted, therefore s0 = 3 and s1, s2 and s3 are set. If there were other elements of "s" already defined, like s5, their values are not changed.
Code:
s5 = -9999
Loop 30
   Random x%A_Index%, 10, 99
ArraySort("s","x",8,10)
MsgBox %s0%`n%s1%`n%s2%`n%s3%`n%s4%`n%s5%
Back to top
View user's profile Send private message
Invalid User



Joined: 14 Feb 2005
Posts: 442
Location: Texas, Usa

PostPosted: Thu Mar 09, 2006 11:02 pm    Post subject: Reply with quote

thank you for your explinations
_________________
my lame sig Smile
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
Laszlo



Joined: 14 Feb 2005
Posts: 4078
Location: Pittsburgh

PostPosted: Thu Mar 09, 2006 11:09 pm    Post subject: Reply with quote

You are welcome. It looks like the description was not clear enough, so I added explanations to the original post, too. Others might understand the function easier now. Thanks for pointing out the need for clarifications.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group