OmYcroN Posted September 10, 2011 Share Posted September 10, 2011 Hi. I need an algorithm for this situation: I start with $arr[5] = [4, 10, 5, 11, 6] Then i add an element 5 so the new array is $arr[6] = [4, 10, 5, 11, 6, 5] The problem is the new array must have unique numbers so the result should be $arr[6] = [4, 10, 6, 11, 7, 5] (5 updated to 6, 6 to 7) I've tried with the recursive method but Autoit shows me "Recursion level has been exceeded - AutoIt will quit to prevent stack overflow". Any ideas ? Link to comment Share on other sites More sharing options...
UEZ Posted September 10, 2011 Share Posted September 10, 2011 What about this? #include <Array.au3> Global $arr[5] = [4, 10, 5, 11, 6] _ArrayAdd($arr, 5) _ArraySort($arr) For $i = 0 To UBound($arr) - 2 If $arr[$i] = $arr[$i + 1] Then $arr[$i + 1] += 1 EndIf Next _ArrayDisplay($arr) Br, UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
OmYcroN Posted September 10, 2011 Author Share Posted September 10, 2011 Thanks for the reply. But what if i have an array like this $arr[n][10] (variable n) and I want to work only with the $arr[n][0] part that has the numbers ?! _ArraySort is not an option in this case. Link to comment Share on other sites More sharing options...
UEZ Posted September 10, 2011 Share Posted September 10, 2011 Same for 2D arrays: #include <Array.au3> Global $arr[6][3] = [ [4, Random(0, 20, 1), Random(0, 20, 1)], _ [10, Random(0, 20, 1), Random(0, 20, 1)], _ [5, Random(0, 20, 1), Random(0, 20, 1)], _ [11, Random(0, 20, 1), Random(0, 20, 1)], _ [6, Random(0, 20, 1), Random(0, 20, 1)], _ [5, Random(0, 20, 1), Random(0, 20, 1)]] _ArraySort($arr, 0, 0, 0, 0) _ArrayDisplay($arr) For $i = 0 To UBound($arr) - 2 If $arr[$i][0] = $arr[$i + 1][0] Then $arr[$i + 1][0] += 1 EndIf Next _ArrayDisplay($arr) Br, UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
OmYcroN Posted September 10, 2011 Author Share Posted September 10, 2011 So I can use $arr[6][3] as $arr[x][y] with the same result ?! Link to comment Share on other sites More sharing options...
czardas Posted September 10, 2011 Share Posted September 10, 2011 (edited) You can add rows or columns using ReDim. However it is advisable to avoid using ReDim and calculate the number of rows and columns you need to declare the array in the first place. Another function you might find useful is _ArrayConcatenate. Here is an example of adding a column to a one dimensional array (turning it into a two dimensional array) without using ReDim. #include <Array.au3> _Run() Func _Run() Local $aMyArray[13] = [12,"one","two","three","four","five", _ "six","seven","eight","nine","ten","eleven","twelve"] _ArrayDisplay($aMyArray) $aMyArray = _InsertStringLenCol($aMyArray) _ArrayDisplay($aMyArray) EndFunc ;==> _Run Func _InsertStringLenCol($aArray) ; Element 0 must contain the number of records => No Error Checks Local $aTemp[$aArray[0] +1][2] $aTemp[0][0] = 0 ; Length of the longest string $aTemp[0][1] = $aArray[0] ; The number of records For $i = 1 To UBound($aArray) -1 $aTemp[$i][0] = StringLen($aArray[$i]) $aTemp[$i][1] = $aArray[$i] If $aTemp[$i][0] > $aTemp[0][1] Then $aTemp[0][1] = $aTemp[$i][0] Next Return $aTemp EndFunc ;==> _InsertStringLenCol The added column gives the string length of each element in the original array. It's just an example. Edited September 10, 2011 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
OmYcroN Posted September 10, 2011 Author Share Posted September 10, 2011 Thank YOU very much for the methods. It works! The _ArraySort() saved me a lot of headache. Link to comment Share on other sites More sharing options...
MvGulik Posted September 10, 2011 Share Posted September 10, 2011 Recursion example.#include <array.au3> main() Func main() Local $a1[5] = [4, 10, 5, 11, 6] test($a1, 5) ReDim $a1[UBound($a1, 1) + 1] $a1[UBound($a1, 1) - 1] = 5 _ArrayDisplay($a1) EndFunc Func test(ByRef $aData, $iAdd) For $i = 0 To UBound($aData, 1) - 1 If $aData[$i] = $iAdd Then $iAdd += 1 test($aData, $iAdd) $aData[$i] = $iAdd EndIf Next EndFunc "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now