hawkair Posted June 17, 2023 Posted June 17, 2023 Hello I have a dynamic array of Points (x,y,Color,OtherInfo...) I want to replace the values of one point which is represented by an array, with other values in one sweep (if possible). I do it like this: Global $Pt = [[1075, 417, 15395562, "Idle0", 1, ""],[1115, 417, 15395562, "Idle1", 1, ""],[1155, 417, 15395562, "Idle2", 1, ""]] Local $tmp = [1040,427,15856113, "Idle0", 1, ""] _ArrayAssign($Pt, 0, $tmp) ;~ _ArrayAssign($Pt, 0, StringSplit('1040,427,15856113, "Idle0", 1, ""', ",", 2)) Exit MsgBox(262144, "Array",_MyArrayToString($Pt, 0, 6)) Func _MyArrayToString(byref $aArray1, $Elt, $NumElts) Local $i, $s = "" For $i = 0 To $NumElts - 1 $s &= $aArray1[$Elt][$i] & "," Next Return $s EndFunc Func _ArrayAssign(byref $aArray1, $Elt, $aArray2) Local $i For $i = 0 To UBound($aArray2) - 1 $aArray1[$Elt][$i] = $aArray2[$i] Next EndFunc (OK, no error checking...) Is there a faster way to do this? (ie sth like $Pt[0] = [1040,427,15856113, "Idle0", 1, ""]) Also how do I write _MyArrayToString if $NumElts is unknown? That is if I dont know the number of elements.
Dan_555 Posted June 17, 2023 Posted June 17, 2023 (edited) currently you are changing only the 1st element of the array in the _ArrayAssign function. (because the $elt is always 0) p.s. you should use easily distinguishable numbers during the test phase. Use _ArrayDisplay to see the content of the array, like this: #include <Array.au3> Global $Pt = [[1075, 417, 15395562, "Idle0", 1, ""],[1115, 417, 15395562, "Idle1", 1, ""],[1155, 417, 15395562, "Idle2", 1, ""]] Local $tmp = [1,2,13, "mood0", 2, ""] _ArrayDisplay($pt) _ArrayAssign($Pt, 0, $tmp) ;~ _ArrayAssign($Pt, 0, StringSplit('1040,427,15856113, "Idle0", 1, ""', ",", 2)) _ArrayDisplay($pt) MsgBox(262144, "Array",_MyArrayToString($Pt)) Func _MyArrayToString(byref $aArray1) Local $i, $j, $s = "" $elt=UBound($aArray1,1) $NumElts=UBound($aArray1,2) For $i = 0 To $Elt- 1 for $j = 0 to $NumElts-1 $s &= $aArray1[$i][$j] & "," Next Next Return $s EndFunc Func _ArrayAssign(byref $aArray1, $Elt, $aArray2) Local $i For $i = 0 To UBound($aArray2) - 1 $aArray1[$Elt][$i] = $aArray2[$i] Next EndFunc Quote Also how do I write _MyArrayToString if $NumElts is unknown? That is if I dont know the number of elements. You have to do it somewhat like this: Func _MyArrayToString(byref $aArray1) Local $i, $j, $s = "" $elt=UBound($aArray1,1) $NumElts=UBound($aArray1,2) For $i = 0 To $Elt- 1 for $j = 0 to $NumElts-1 $s &= $aArray1[$i][$j] & "," Next Next Return $s EndFunc Autoit already has some built in functions to deal with the arrays, like: _ArrayFromString() _ArrayToString() _ArrayConcatenate() and others, maybe you can use them instead of writing your own ? Edited June 17, 2023 by Dan_555 Some of my script sourcecode
hawkair Posted June 17, 2023 Author Posted June 17, 2023 (edited) Dan_555 Thank you for your quick response. I woud like to make some points: 4 hours ago, Dan_555 said: currently you are changing only the 1st element of the array in the _ArrayAssign function. (because the $elt is always 0) Yes I do because I want to demonstrate what I want. I know that to change the 3rd Point I should use this call _ArrayAssign($Pt, 2, $tmp) 4 hours ago, Dan_555 said: Use _ArrayDisplay _ArrayDisplay displays the whole array. I actually was interested in displaying just a selected Point which If I undestand correctly represents a row in the array. Lastly the use of Ubound could be what I needed. What would happen if one or more points have different number elements? for example if the 3rd Pt was [1155, 417, 15395562, "Idle2", 1, "","ClickPt1"] with 7 Elements. I checked and Ubound($Pt,2) returns 7 - the maximum number so it would fail if I wanted to work on Pt1 or $Pt2 which have 6 Elements I am sorry that I was not clear in describing what I want. So if its clearer, lets Imagine we have a two dimensional array with rows and columns. The questions are: 1.Can I change a whole Row with a single command or does it have to be with a function like I am Using? Is there a shorter version of this function? 2.Can I display a whole Row with a single command or does it have to be with a function like I am Using? Is there a shorter version of this function? 3.If not all rows have the same number of columns (and Ubound(Array,2) does not work) how could I check the number of columns of my Row? Is there something like: On error goto X, Array[r][c] = Val, and when c is out of bounds instead of the program crashing it transfers control to X? (Edit: I was too quick to respond and then I took a closer look at _ArrayDisplay and found that it can in fact help with number 2: ie $p = 1 _ArrayDisplay($Pt, "",$p & ":" & $p) ;~ This displays the 2nd Point or Row ;~ and if I don't like the Output of _ArrayDisplay I can use MsgBox (262144, "Point " & $p, _ArrayToString($Pt, ",", $p, $p)) ;~ This displays the 2nd Point or Row in a nicer way (Edit no 2. After digging into _ArraytoString code and other Array functions to see how they tackled the problem of variable columns I came to the realization that the Whole array is reorganized to have the maximum number of columns that appear in any row. The Array functions view/display the missing/nondefined columns as Blank "") However I think I still dont have answers to questions 1 and 3. Edited June 17, 2023 by hawkair
Solution Dan_555 Posted June 17, 2023 Solution Posted June 17, 2023 (edited) Hi, i do not know if it is possible to replace all the wanted items in one sweep, but what you could do is to delete one entry from an array and to insert a new one into it, something like: #include <Array.au3> Global $Pt = [[1075, 417, 15395562, "Idle0", 1, ""],[1115, 417, 15395562, "Idle1", 1, ""],[1155, 417, 15395562, "Idle2", 1, ""]] _ArrayDisplay($pt) _arrayDelete ($pt,1) _arrayInsert($pt,1,'1040|427|15856113|Idle0|1|') _ArrayDisplay($pt) Edited June 17, 2023 by Dan_555 Some of my script sourcecode
jchd Posted June 17, 2023 Posted June 17, 2023 3 hours ago, hawkair said: 3.If not all rows have the same number of columns (and Ubound(Array,2) does not work) how could I check the number of columns of my Row? By constructions, Array datatype are fixed-bounded from their declaration: only the number of rows can evolve by inserting/deleting rows. This implies that every row has the same number of columns. In case that limitation can't fit your needs, you can switch to another data structure, for instance an array (or a Map) where one column (resp. the value) is itself a variable-size array. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
hawkair Posted June 18, 2023 Author Posted June 18, 2023 Thank you Dan_55 and jchd for the explanation.
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