Jump to content
Sign in to follow this  
litlmike

Reverse a 2-Dimensional Array

Recommended Posts

litlmike

I have an issue that I can think of solving in 1 or 2 ways. 1) Flip a 2-Dimensional array and problem solved, or 2) Loop through the array 'backwards'.

For instance, if I want to reverse the data in a 2-dimensional Array, or in other words, flip the data in the array upside down. So if the array has 30 elements in the 1st dimension, and those elements read [30, 29, 28 ... 1]; then I want those 30 elements to now read [1 ... 28, 29, 30]. Likewise, if the 2nd dimension reads [A, B, C ... Z], then it should be flipped to read [Z ... C, B, A]. But, I don't know of any way to accomplish this.

The other way I can think of doing it is to loop through the array 'backwards', starting with the last element and ending with the first. I was wondering what the AutoIt community would think is the better solution, and give me an idea of the best way to do this?

At the bottom of the post, I have included a reproducer of the project I am working on.

Normally I use this Psuedo-Model to Loop through a 2-D array.

For $xCC=0 To UBound($aTableData,2)-1
        $Row=1
        For $iCC=1 To UBound($aTableData)-1
;~  ;Write Data to Xls.
            _ExcelWriteCell($oExcel, $aTableData[$iCC][$xCC], $Row, $Column)
            $Row+=1
        Next
        $Column+=1
    Next

;;;;;;;;;;;;;;;;;;;;;;;;;Declare Stuff;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#include <IE.au3>
#include <ExcelCOM_UDF.au3>
#include <Array.au3>

$sFilePath = @ScriptDir & "\DA_Data.xls"
$sUrl = "about:blank"
$oIE = _IECreate($sUrl,1)
Global $Row
$Column=1
;;;;;;;;;;;;;;;;;;;;;;;;;End Declare Stuff;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;Make the IE Page reproducer
Make_IE_Page()

;Get Table Data from Webpage
$oTable = _IETableGetCollection ($oIE, 0)
$aTableData = _IETableWriteToArray ($oTable, True)

;Make XLS file
$oExcel = _ExcelBookNew()

;Put Data into XLS
    For $xCC=0 To UBound($aTableData,2)-1
        $Row=1
        For $iCC=1 To UBound($aTableData)-1
;~          ;Write Data to Xls.
            _ExcelWriteCell($oExcel, $aTableData[$iCC][$xCC], $Row, $Column)
            $Row+=1
        Next
        $Column+=1
    Next

    $oExcel.Rows.AutoFit
    $oExcel.Columns.AutoFit
    _ExcelBookSaveAs($oExcel, $sFilePath, "xls", 0, 1)

Func Make_IE_Page()
    ;Write HTML
    $s_HTML1 = '<table border=0 cellpadding=0 cellspacing=2>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Thu, Feb 28</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="33">&nbsp;    973&nbsp;<font color="990000">(<strong>4</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Wed, Feb 27</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="30">&nbsp;    887&nbsp;<font color="990000">(<strong>11</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Tue, Feb 26</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="39">&nbsp;  1,146&nbsp;<font color="990000">(<strong>15</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Mon, Feb 25</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="52">&nbsp;  1,532&nbsp;<font color="990000">(<strong>12</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Sun, Feb 24</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="38">&nbsp;  1,099&nbsp;<font color="990000">(<strong>9</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Sat, Feb 23</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="38">&nbsp;  1,117&nbsp;<font color="990000">(<strong>11</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Fri, Feb 22</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="43">&nbsp;  1,252&nbsp;<font color="990000">(<strong>7</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Thu, Feb 21</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="36">&nbsp;  1,050&nbsp;<font color="990000">(<strong>14</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Wed, Feb 20</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="33">&nbsp;    960&nbsp;<font color="990000">(<strong>7</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Tue, Feb 19</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="41">&nbsp;  1,208&nbsp;<font color="990000">(<strong>9</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Mon, Feb 18</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="45">&nbsp;  1,313&nbsp;<font color="990000">(<strong>9</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Sun, Feb 17</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="32">&nbsp;    946&nbsp;<font color="990000">(<strong>5</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Sat, Feb 16</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="7">&nbsp;    200&nbsp;<font color="990000">(<strong>0</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Fri, Feb 15</nobr></td>' ;& _

    $s_HTML2 = '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="38">&nbsp;  1,127&nbsp;<font color="990000">(<strong>7</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Thu, Feb 14</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="39">&nbsp;  1,137&nbsp;<font color="990000">(<strong>10</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Wed, Feb 13</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="37">&nbsp;  1,086&nbsp;<font color="990000">(<strong>10</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Tue, Feb 12</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="48">&nbsp;  1,419&nbsp;<font color="990000">(<strong>13</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Mon, Feb 11</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="41">&nbsp;  1,214&nbsp;<font color="990000">(<strong>10</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Sun, Feb 10</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="200">&nbsp;  5,860&nbsp;<font color="990000">(<strong>9</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Sat, Feb 9</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="40">&nbsp;  1,158&nbsp;<font color="990000">(<strong>10</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Fri, Feb 8</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="35">&nbsp;  1,033&nbsp;<font color="990000">(<strong>5</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Thu, Feb 7</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="45">&nbsp;  1,317&nbsp;<font color="990000">(<strong>12</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Wed, Feb 6</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="50">&nbsp;  1,458&nbsp;<font color="990000">(<strong>13</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Tue, Feb 5</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="62">&nbsp;  1,819&nbsp;<font color="990000">(<strong>11</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Mon, Feb 4</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="43">&nbsp;  1,249&nbsp;<font color="990000">(<strong>6</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Sun, Feb 3</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="42">&nbsp;  1,243&nbsp;<font color="990000">(<strong>9</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Sat, Feb 2</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="56">&nbsp;  1,632&nbsp;<font color="990000">(<strong>5</strong>)</font></td>' & _
    '</tr>' & _
    '<tr>' & _
    '<td align="right" class="smplain"><nobr>Fri, Feb 1</nobr></td>' & _
    '<td class="smplain"><img src="images/graph_ht1.gif" align="absmiddle" border="1" height="12" width="52">&nbsp;  1,537&nbsp;<font color="990000">(<strong>14</strong>)</font></td>' & _
    '</tr>' & _
    '</table>'

    _IEDocWriteHTML( $oIE, $s_HTML1 & $s_HTML2)
EndFunc

TIA

Edited by litlmike

Share this post


Link to post
Share on other sites
ChrisL
weaponx

#include <Array.au3>

Dim $array[5][2]

$array[0][0] = 0
$array[0][1] = "zero"

$array[1][0] = 1
$array[1][1] = "one"

$array[2][0] = 2
$array[2][1] = "two"

$array[3][0] = 3
$array[3][1] = "three"

$array[4][0] = 4
$array[4][1] = "four"

;_ArrayDisplay($array)

;Reverse array
$array = _Reverse2DArray($array)

_ArrayDisplay($array)

Func _Reverse2DArray(ByRef $aArray)
    $rows = Ubound($aArray)
    $columns = Ubound($aArray, 2)
    
    Local $aTemp[$rows][$columns]
    
    For $Y = 0 to $rows-1
        ConsoleWrite("Row " & $Y & ": ")
        
        For $X = 0 to $columns-1
            $aTemp[$Y][$X] = $aArray[$rows - $Y - 1][$X]
            
            ConsoleWrite("Column " & $X & ": " & $aArray[$Y][$X] & ", ")
        Next
        ConsoleWrite(@CRLF)
    Next
    Return $aTemp
EndFunc

Share this post


Link to post
Share on other sites
litlmike

#include <Array.au3>

Dim $array[5][2]

$array[0][0] = 0
$array[0][1] = "zero"

$array[1][0] = 1
$array[1][1] = "one"

$array[2][0] = 2
$array[2][1] = "two"

$array[3][0] = 3
$array[3][1] = "three"

$array[4][0] = 4
$array[4][1] = "four"

;_ArrayDisplay($array)

;Reverse array
$array = _Reverse2DArray($array)

_ArrayDisplay($array)

Func _Reverse2DArray(ByRef $aArray)
    $rows = Ubound($aArray)
    $columns = Ubound($aArray, 2)
    
    Local $aTemp[$rows][$columns]
    
    For $Y = 0 to $rows-1
        ConsoleWrite("Row " & $Y & ": ")
        
        For $X = 0 to $columns-1
            $aTemp[$Y][$X] = $aArray[$rows - $Y - 1][$X]
            
            ConsoleWrite("Column " & $X & ": " & $aArray[$Y][$X] & ", ")
        Next
        ConsoleWrite(@CRLF)
    Next
    Return $aTemp
EndFunc
Now that is just lovely. Why is _Reverse2DArray(ByRef $aArray) not in Array.au3?

Thanks

Share this post


Link to post
Share on other sites
weaponx

Now that is just lovely. Why is _Reverse2DArray(ByRef $aArray) not in Array.au3?

Thanks

Because I just wrote it now?

Note: The ByRef isn't actually needed.

Edited by weaponx

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  

×