Jump to content

Reverse an array


Go to solution Solved by mikell,

Recommended Posts

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 by MyEarth
Link to comment
Share on other sites

Not tested:

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

#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

How many time i need to say that i need to preserve the index zero? :D

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 by MyEarth
Link to comment
Share on other sites

#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

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 by czardas
Link to comment
Share on other sites

_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 by MyEarth
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...