MyEarth Posted March 30, 2015 Share Posted March 30, 2015 (edited) With 2D array, how i can reverse all the row of all column like the first value become last, the last the first? I need to start from the first row and avoid the 0 index #include <Array.au3> Global $avArray[11][2] $avArray[0][0] = "" $avArray[0][1] = "" $avArray[1][0] = 1 $avArray[2][0] = 2 $avArray[3][0] = 3 $avArray[4][0] = 4 $avArray[5][0] = 5 $avArray[6][0] = 6 $avArray[7][0] = 7 $avArray[8][0] = 8 $avArray[9][0] = 9 $avArray[10][0] = 10 $avArray[1][1] = "J" $avArray[2][1] = "I" $avArray[3][1] = "H" $avArray[4][1] = "G" $avArray[5][1] = "F" $avArray[6][1] = "E" $avArray[7][1] = "D" $avArray[8][1] = "C" $avArray[9][1] = "B" $avArray[10][1] = "A" _ArrayDisplay($avArray) Many thanks. Oh, avoid the zero index doesn't mean i want to remove, i want to preserve from the reversing, a function with a parameter like $iStart = 1 Edited March 30, 2015 by MyEarth Link to comment Share on other sites More sharing options...
funkey Posted March 30, 2015 Share Posted March 30, 2015 Not tested: Programming today is a race between software engineers striving tobuild bigger and better idiot-proof programs, and the Universetrying to produce bigger and better idiots.So far, the Universe is winning. Link to comment Share on other sites More sharing options...
MyEarth Posted March 30, 2015 Author Share Posted March 30, 2015 I have tested it, that reverse all the array but i want to preserve the 0 index from the reversing, a function with a parameter like $iStart = 1. So need a small change or a total rewrite, dunno. Link to comment Share on other sites More sharing options...
mikell Posted March 30, 2015 Share Posted March 30, 2015 expandcollapse popup#include <Array.au3> Global $avArray[11][2] $avArray[0][0] = "" $avArray[0][1] = "" $avArray[1][0] = 1 $avArray[2][0] = 2 $avArray[3][0] = 3 $avArray[4][0] = 4 $avArray[5][0] = 5 $avArray[6][0] = 6 $avArray[7][0] = 7 $avArray[8][0] = 8 $avArray[9][0] = 9 $avArray[10][0] = 10 $avArray[1][1] = "J" $avArray[2][1] = "I" $avArray[3][1] = "H" $avArray[4][1] = "G" $avArray[5][1] = "F" $avArray[6][1] = "E" $avArray[7][1] = "D" $avArray[8][1] = "C" $avArray[9][1] = "B" $avArray[10][1] = "A" _ArrayDisplay($avArray) $a = _Reverse2DArray($avArray) _ArrayDisplay($a) Func _Reverse2DArray($aArray) $rows = Ubound($aArray) $columns = Ubound($aArray, 2) Local $aTemp[$rows][$columns] For $Y = 1 to $rows-1 For $X = 0 to $columns-1 $aTemp[$Y][$X] = $aArray[$rows - $Y ][$X] Next Next Return $aTemp EndFunc Link to comment Share on other sites More sharing options...
MyEarth Posted March 30, 2015 Author Share Posted March 30, 2015 (edited) How many time i need to say that i need to preserve the index zero? Change the first row with this $avArray[0][0] = "ASD" $avArray[0][1] = "ASD" And you will see that will be deleted after reversing. A parameter like $iStart = 1 was the best solution, so other can use it. Thanks Edited March 30, 2015 by MyEarth Link to comment Share on other sites More sharing options...
mikell Posted March 30, 2015 Share Posted March 30, 2015 expandcollapse popup#include <Array.au3> Global $avArray[11][2] $avArray[0][0] = "some" $avArray[0][1] = "thing" $avArray[1][0] = 1 $avArray[2][0] = 2 $avArray[3][0] = 3 $avArray[4][0] = 4 $avArray[5][0] = 5 $avArray[6][0] = 6 $avArray[7][0] = 7 $avArray[8][0] = 8 $avArray[9][0] = 9 $avArray[10][0] = 10 $avArray[1][1] = "J" $avArray[2][1] = "I" $avArray[3][1] = "H" $avArray[4][1] = "G" $avArray[5][1] = "F" $avArray[6][1] = "E" $avArray[7][1] = "D" $avArray[8][1] = "C" $avArray[9][1] = "B" $avArray[10][1] = "A" _ArrayDisplay($avArray) $a = _Reverse2DArray($avArray, 1) _ArrayDisplay($a) Func _Reverse2DArray($aArray, $start = 0) $rows = Ubound($aArray) $columns = Ubound($aArray, 2) Local $aTemp = $aArray For $Y = $start to $rows-1 For $X = 0 to $columns-1 $aTemp[$Y][$X] = $aArray[$rows-1 - $Y + $start][$X] Next Next Return $aTemp EndFunc Link to comment Share on other sites More sharing options...
czardas Posted March 30, 2015 Share Posted March 30, 2015 (edited) There is normally no reason to reverse an array. It usually makes more sense to access elements as if they were in reverse order. A typical method for accessing the data in reverse order would be to loop backwards from $aArray[0] To 1 Step -1 [or use Ubound($aArray) To 0 Step -1]. Edited March 30, 2015 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
mikell Posted March 30, 2015 Share Posted March 30, 2015 czardas, While knowing this method I'm pretty sure I saw a kind of _ArrayReverse() somewhere in the helpfile Link to comment Share on other sites More sharing options...
MyEarth Posted March 30, 2015 Author Share Posted March 30, 2015 (edited) _ArrayReverse is only for 1D array ( Failure - 0, sets @error: $avArray is not a 1 dimensional array ) What is the best method? Accessing the array in the same way by mikell/weaponx or the _arrayreverse "style" so using Step -1? Or is the same? Edited March 30, 2015 by MyEarth Link to comment Share on other sites More sharing options...
Solution mikell Posted March 30, 2015 Solution Share Posted March 30, 2015 The best method as czardas said is by far to leave your array untouched and loop backwards if needed ArrayReverse (in fact any array transformation) should be used as less as possible czardas 1 Link to comment Share on other sites More sharing options...
MyEarth Posted March 30, 2015 Author Share Posted March 30, 2015 (edited) Ok, thanks to all Edited March 30, 2015 by MyEarth 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