Sign in to follow this  
Followers 0
Dirgi

_ArrayMinX() function (based on _ArrayMin() function)

1 post in this topic

#1 ·  Posted (edited)

_ArrayMinX() is an extension (I wrote) of the _ArrayMin() function.

Just beforehand: I'm using now AutoIT intemsively for just one week - So please have mercy when something is wrong or not done in the right way.

Why _ArrayMinX?:

When I used the _ArrayMin() function I did not receive the minimum value of my array caused to the issue that my array had some positions without any content.I allways received an empty value.

After some research on the internet I didn't find any answer to solve the problem, so I started to write my own function.

How does _ArrayMinX() work ?:

In a short sentence: When there is a MinValue, you'll receive it back :mellow:

I'll explain this with the following example:

This is my example Array:

Local $ExampleArray[19] = [2,"2",4,"25","0.34374374",1,6,"","A","x","","",12,70,"3","","7A","FE6","This is a test"]

As you can see, it contents a mix of numbers, alpanumerics and empty values like ""

When using now the _ArrayMin() function you'll always receive an empty value back which

might be correct from the computer calculation perspective but not really whant I want.

I would like to know the real minimum existing value, which can be i.e. a "1" or a "A"

So what does my function do?

1. Build a uniqueness with the function _ArrayUnique() to have each value just once.

(even an empty array appears now just once)

2. Sort the array with the _ArraySort() function. If there's now an empty Array it will

be the first in the sorted list ;-)

3. Now the function check if an empty array is existing and place the pointer on the right array

4. When asking for the Min Value I can additionally decide if I'd like to have my focus on an

numeric ar alpanumeric value.

Feel free to use the script.

I did already an implementation of the function on my <Array.au3> base script.

If you'd like to have it as well I just attached the content copy of my updated <Array.au3> script.

Cheers, Dirgi

Example Script: :)

#include <Array.au3>
Local $ExampleArray[19] = [2,"2",4,"25","0.34374374",1,6,"","A","x","","",12,70,"3","","7A","FE6","This is a test"]
Local $X
$X= _ArrayMinX($ExampleArray,0) ;Call the function
Msgbox (0,"",$X)
 
;---------------------------------------------------------------------------------------------------
 
Func _ArrayMinX(Const ByRef $AvArray, $iCompNumeric = 0 )
If Not IsArray($avArray) Then Return SetError(1, 0, -1)
Local $ArrayPointer, $AminX
$AminX = _ArrayUnique($avArray)     ;Compress the numbers of same contents (even arrays without content)
_ArraySort($AminX)                  ;Array without content (if existing) appears on top of sorted list ;-)
$ArrayPointer = 0
If $AminX[0] = "" Then $ArrayPointer = 1        ;If Array[0] has no content then set pointer to Array[1]
$AminX = _ArrayMin($AminX, $iCompNumeric, $ArrayPointer)
If @error Then Return SetError(@error, 0, "")
Return $AminX
EndFunc

Here is the implementation into <Array.au3>: (Only my part)

; #FUNCTION# ====================================================================================================================
; Name...........: _ArrayMinX
; Description ...: Returns the lowest value held in an array, even when there are some arrays without any content
; Syntax.........: _ArrayMinX(Const ByRef $avArray[, $iCompNumeric = 0)
; Parameters ....: $avArray     - Array to search
;               $iCompNumeric - [optional] Comparison method:
;               |0 - compare alphanumerically
;               |1 - compare numerically
; Return values .: Success - The minimum value in the array
;               Failure - "", sets @error (see _ArrayMinIndex() description for error codes)
; Author ........: Dirgi
; Modified.......:
; Remarks .......: Based on the scripting of the related functions
; Related .......: _ArraySort, _ArrayMinIndex, _ArrayMin, _ArrayUnique
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _ArrayMinX(Const ByRef $AvArray, $iCompNumeric = 0 )
If Not IsArray($avArray) Then Return SetError(1, 0, -1)
Local $ArrayPointer, $AminX
$AminX = _ArrayUnique($avArray)     ;Compress the numbers of same contents (even arrays without content)
_ArraySort($AminX)                  ;Array without content (if existing) appears on top of sorted list ;-)
$ArrayPointer = 0
If $AminX[0] = "" Then $ArrayPointer = 1        ;If Array[0] has no content then set pointer to Array[1]
$AminX = _ArrayMin($AminX, $iCompNumeric, $ArrayPointer)
If @error Then Return SetError(@error, 0, "")
Return $AminX
EndFunc

The complete updated <ARRAY.AU3> you'll find attached as compressed zip file.

ArrayAU3.zip

Edited by Dirgi

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • Eggsplorer
      By Eggsplorer
      Hi,
      Ich hab ein Script für Windows Updates, dass nach dem OOBE aufgerufen werden soll. Run/RunOnce ist daher nicht möglich.
      Stattdessen möchte ich das Script in den Autostart schreiben lasse. Wenn die OOBE beendet wird und der Administrator angemeldet wird, soll das Update-Script einmalig aufgerufen werden und sich anschl. selbst löschen.
      Lang rede, kurzer Sinn:
      Wie kann ich folgendes Script in den Autostart schreiben?
      RunWait(@ComSpec & " /c " & "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File C:\UA\Scripts\win-updates.ps1 -MaxUpdatesPerCycle 100 -RestartRequired 1", "") Ich würde ungern eine BAT oder EXE reinkopieren, da ich es unsauber finde. Die müsste ja auch zusätzlich irgendwo liegen.
      Danke
      Eggsplorer
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good morning everyone
      I worked a bit on a little project of mine, and I managed to do what I was trying to do, or, automate a quiz in AutoIt.
      Now, everything works fine but, in order to achieve what I'm trying to achive, I'd like to make this script run faster.
      I'll attach a portion of the code in order to let you understand what my script does.
      $objIE_h1 = _IETagNameGetCollection($objIE, "h1") ; Take the text of the answer If @error Then ConsoleWrite("Errore durante la lettura degli h1 nella pagina. - Errore: " & @error & @CRLF) Else For $objElement In $objIE_h1 $strInnerText = $objElement.innerText Next Select Case StringInStr($strInnerText, "QUESTION") ; Here is the question ; PS4 PRO $objSpans = _IETagNameGetCollection($objIE, "span") ; Answers are stored here For $objElement In $objSpans If $i >= 4 Then $strAnswer = $objElement.innerText ; Take the text of the answer n° 0, 1, 2, 3 If StringInStr($strAnswer, "CORRECT ANSWER") Then ; If I match the correct answer, then, I have to click on the input with answer id = answer_i $j = $i - 4 $objInput = _IEGetObjById($objIE, "answer_" & $j) If @error Then ConsoleWrite("Errore durante la lettura della risposta #" & $i & " - Errore: " & @error & @CRLF) Else _IEAction($objInput, "click") If @error Then ConsoleWrite("Errore durante il click sull'elemento. - Errore: " & @error) Else ConsoleWrite($j & ") Risposta: " & $strAnswer & @CRLF) ExitLoop EndIf EndIf EndIf $i+=1 Else $i+=1 EndIf Next $i = 0 $j = 0 This is what my script does.
      Questions are 20, in which I have 15 multiple choice question, 2 images ( on which I use _IEImgClick() ), and 3 text inputs ( a simple send with the text I have to enter ).
      How can I make all of these, faster?
      If I reach this goal, I've probably done one of my best goals in life, and I'd like to share my happiness with you, giving a huge donation to this fantastic community!
      Thanks for your help
    • czardas
      By czardas
      Haven't had much time to code recently. However the following thread inspired me.
      The debate about linear, parallel and binary search methods was rather interesting and, in an attempt to be diplomatic, I decided to combine @jchd's suggestion with @LarsJ's binary search example. I decided that the binary search algorithm required modification to make it more linear. As usual, 'if you invent something, it probably already exists and if it already exists, it exists for a reason'. My first attempt was not all that good. The code worked but was really a mess. I blame peer pressure (to post an example of a parallel search method).  I will delete that old code in due course.
      With a little memory jogging and a glance at the help file, the solution turned out to be quite easy: I just needed a better understanding of Euler. Further modification will be needed to work with more complicated unicode strings. The output could be returned as an array or a delimitered string. I'm not so interested in those details. I'm just going to post the algorithm for now and anyone, who wants to, can modify it to suit their needs. Both arrays must contain at least 1 element.
      Local $aFoo = [0,1,2,3,4,5,6,7,9,10,11,12,13,14,15,16,19,20,23,24,26,30,35,39,40,41] Local $aBar = [0,1,5,6,7,8,9,10,11,12,13,14,17,18,19,21,24,25,26,27,34,35,38,40] ParallelExponetialSearch($aFoo, $aBar) ; Compares two lists - returning positive matches. Each input array must be unique (individually) and in alphabetical order. Func ParallelExponetialSearch($aFoo, $aBar) Local $sFind, _ $iMin_F = -1, $iMax_F = UBound($aFoo) -1, $Lo_F = $iMin_F, $Hi_F, _ $iMin_B = -1, $iMax_B = UBound($aBar) -1, $Lo_B = $iMin_B, $Hi_B While $iMin_F < $iMax_F And $iMin_B < $iMax_B ; Toggle Arrays - Which array has most untested elements? This is the one we want to search next, ; so we can bypass more comparisons because (in theory) mismatches have a greater chance of being skipped. If $iMax_F - $iMin_F >= $iMax_B - $iMin_B Then ; $aFoo has more (or an equal number of) untested elements $Hi_F = $iMax_F $iMin_B += 1 $sFind = $aBar[$iMin_B] While $Lo_F < $Hi_F ; search $aFoo For $i = 0 To Floor(Log($Hi_F - $Lo_F) / Log(2)) $Lo_F = $iMin_F + 2^$i If $aFoo[$Lo_F] = $sFind Then $iMin_F = $Lo_F ; each match should be added to the output [perhaps an array] ConsoleWrite($sFind & " found at $aFoo[" & $Lo_F & "] = $aBar[" & $iMin_B & "]" & @LF) ExitLoop 2 ElseIf $aFoo[$Lo_F] > $sFind Then $Hi_F = $Lo_F -1 $iMin_F += Floor(2^($i -1)) $Lo_F = $iMin_F ContinueLoop 2 EndIf Next $iMin_F = $Lo_F ; minimum increment is one WEnd Else ; $aBar has more untested elements $Hi_B = $iMax_B $iMin_F += 1 $sFind = $aFoo[$iMin_F] While $Lo_B < $Hi_B ; search $aBar For $i = 0 To Floor(Log($Hi_B - $Lo_B) / Log(2)) $Lo_B = $iMin_B + 2^$i If $aBar[$Lo_B] = $sFind Then $iMin_B = $Lo_B ; each match should be added to the output [perhaps an array] ConsoleWrite($sFind & " found at $aFoo[" & $iMin_F & "] = $aBar[" & $Lo_B & "]" & @LF) ExitLoop 2 ElseIf $aBar[$Lo_B] > $sFind Then $Hi_B = $Lo_B -1 $iMin_B += Floor(2^($i -1)) $Lo_B = $iMin_B ContinueLoop 2 EndIf Next $iMin_B = $Lo_B ; minimum increment is one WEnd EndIf WEnd EndFunc ;==> ParallelExponetialSearch I hope this will be useful to someone. I believe it deserved a thread of its own!
    • RC86
      By RC86
      Afternoon!
      I have a requirement to return both a string and an array from a function so as a result I put them both into an array and returned that.  I can access them in their entirety after returning them but then I can't seem to access the array elements after this.  Should I be able to or is there a prettier way?
      #include <Array.au3> ;Memory info returned as a string and an array $memoryInfo = _getMemoryInfo() msgbox(0,"Memory Info",$memoryInfo[0]) _ArrayDisplay($memoryInfo[1],"Memory as an Array") Local $newArray[7] $memoryInfo[1] = $newArray msgbox(0,"Test element",$newArray[0]) _ArrayDisplay($newArray) Func _getMemoryInfo() Local $newArray[7] Local $array = MemGetStats() $newArray[0] = $array[0] ;% of memory in use $newArray[1] = Round($array[1]/1024 * 0.001,2) ;Total physical RAM $newArray[2] = Round($array[2]/1024 * 0.001,2) ;Availaible physical RAM $newArray[3] = Round($array[3]/1024 * 0.001,2) ;Total pagefile $newArray[4] = Round($array[4]/1024 * 0.001,2) ;Available pagefile $newArray[5] = Round($array[5]/1024 * 0.001,2) ;Total virtual $newArray[6] = Round($array[6]/1024 * 0.001,2) ;Available virtual $memoryUsage = $newArray[1] - $newarray[2] $pagefileUsage = $newArray[3] - $newarray[4] ;Output/Return Local $returnArray[2] $returnArray[0] = "Memory: " & $memoryUsage & " GB/" & $newArray[1] & " GB " & @CRLF & "Pagefile: " & $pagefileUsage & " GB/" & $newArray[3] & " GB " $returnArray[1] = $newArray return $returnArray EndFunc A bit messy but hopefully it's understandable what I'm trying to achieve.
      Thanks!
    • au3leecher
      By au3leecher
      Hellow everybody,
      I tryed to manipulate a game called "Assault Cube" which is a easy beginning in coding. I run the code but the wished value doesn't change, i don't understand why.
      The Pointers are fine, I checked that the pointers don't change every time while restart the game.
      My Code:
      <snip>
      _____________________________________________________________________________________________________________________________________________________________________________________________
      Checklist:
      The Offsets are 18C & 4
      In Cheat Engine: ("ac_client.exe"+0010F418) <- Pointer
      4 Bytes = 'dword'