Map Index to 2D Array

Recommended Posts

I was reading a recent post about converting a 2D array into a 1D array.  The OP, from my understanding, more-or-less wanted to extract a single column of data and format it in a 1D array.  The solutions were proper and efficient compared to what I wanted to share, but I figured I'd share as it's a good lesson and a neat, purely mathematical approach to mapping an index to a 2D array.  As-is, this only works for 2D arrays.

By knowing how big a 2D array is, an index can be calculated for each indices/subscript.  The functions below define a random sized 2D array, and calculate an index that matches the indices on the 2D array.

```#include "Array.au3"
\$iRow = Random(2,10,1)
\$iCol = Random(2,10,1)
\$iRandomIndex = Random(0,(\$iRow*\$iCol)-1,1)

\$sIndicies = _IndexTo2DIndicies(\$iRandomIndex, \$iRow, \$iCol)
_2DArrayTo1DIndexes(\$iRow, \$iCol, \$iRandomIndex & " = \$aArr" & \$sIndicies)

Func _2DArrayTo1DIndexes(\$iRow = 1, \$iCol = 1, \$sMsg = Default)
\$iRow = Int(\$iRow)
\$iCOl = Int(\$iCol)

For \$iY = 0 to \$iRow - 1
For \$iX = 0 to \$iCol - 1
Local \$iIndex
;Index = (\$iColMax * \$iY) + \$iX
\$aData[\$iY][\$iX] = (\$iCol * \$iY) + \$iX
Next
Next

If \$sMsg = Default Then \$sMsg = "1D Indexes of 2D Array Indicies"
EndFunc

Func _IndexTo2DIndicies(\$iIndex = 0, \$iRow = 0, \$iCol = 0)
\$iIndex = Int(\$iIndex)
\$iRow = Int(\$iRow)
\$iCol = Int(\$iCol)

If \$iIndex = 0 Then Return "[0][0]"
If \$iIndex >= \$iRow * \$iCol Then Return SetError(1,0,0)

Local \$iX = Mod(\$iIndex, \$iCol)
;Local \$iY = (\$iIndex-\$iX) / \$iCol
Local \$iY = Int(\$iIndex / \$iCol)    ;Thanks Chimp

Return "[" & \$iY & "][" & \$iX & "]"
EndFunc```

I used this type of approach before with a pixel array.  I had a 1D array for storing individual pixel information, then mapped the pixels to an X|Y grid (2D array or XY coords).

Now if the purpose is just to flatten an array, the most efficient way I am aware of would be to create a new 1D array the size of the total number of indices in the 2D array (RowMax * ColMax), and loop through the array.  I suppose these methods (above) would be particularly useful if you need to randomly or programmatically access indices in a non-linear fashion.

Anyways, just wanted to share a fun exercise with arrays.

Edited by spudw2k

Spoiler

Misc Code Snippets:
Projects: SubnetCalc
Cool Stuff:

Share on other sites

\$iY can also be calculated like this:

`Local \$iY = Int(\$iIndex / \$iCol)`

• 1

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share on other sites

You're right, and it seems slightly faster.  Good idea.  Implemented.

Spoiler

Misc Code Snippets:
Projects: SubnetCalc
Cool Stuff:

Create an account

Register a new account

• Similar Content

• By rm4453
I have a table I am parsing, to find specific vehicle information. I am unable to get _ArrayFindAll to return the only valid result with my test data.
Below is a sample of the table's HTML:
<td class="textCenter">2010</td> <td>TOYOTA</td> <td>TACOMA 4X4 DB</td> <td></td> <td>BLACK</td> <td class="textCenter">C</td> <td class="textCenter">6</td> <td>GAS</td> <td class="textCenter">A</td> <td class="textCenter">4X4</td> <td class="textCenter">Y</td> <td>CD</td> <td class="textCenter">HT</td> <td class="textCenter">C</td> <td class="textRight" nowrap="nowrap">80,975</td> <td class="textRight" nowrap="nowrap">\$16,800</td> </tr> <tr><!-- 308 --> <td class="textCenter">2010</td> <td>TOYOTA</td> <td>TACOMA 4X4 RG</td> <td></td> <td>BLACK</td> <td class="textCenter">R</td> <td class="textCenter">4</td> <td>GAS</td> <td class="textCenter">5</td> <td class="textCenter">4X4</td> <td class="textCenter">A</td> <td>CD</td> <td class="textCenter">HT</td> <td class="textCenter">C</td> <td class="textRight" nowrap="nowrap">95,224</td> <td class="textRight" nowrap="nowrap">\$9,500</td> </tr> <tr><!-- 309 --> <td class="textCenter">2011</td> <td>BUICK</td> <td>REGAL</td> <td>CXL RL4</td> <td>BLACK</td> <td class="textCenter">4</td> <td class="textCenter">4</td> <td>GAS</td> <td class="textCenter">A</td> <td class="textCenter"></td> <td class="textCenter">A</td> <td>CD</td> <td class="textCenter">SR</td> <td class="textCenter">L</td> <td class="textRight" nowrap="nowrap">102,694</td> <td class="textRight" nowrap="nowrap">\$5,000</td> </tr> <tr><!-- 310 --> <td class="textCenter">2011</td> <td>CHEVROLET</td> <td>AVALANCH 4X4 CR</td> <td>LS</td> <td>GRAY</td> <td class="textCenter">C</td> <td class="textCenter">8</td> <td>E</td> <td class="textCenter">A</td> <td class="textCenter">4X4</td> <td class="textCenter">A</td> <td>CD</td> <td class="textCenter">HT</td> <td class="textCenter">C</td> <td class="textRight" nowrap="nowrap">64,759</td> <td class="textRight" nowrap="nowrap">\$16,300</td> </tr> <tr><!-- 311 --> <td class="textCenter">2011</td> <td>CHEVROLET</td> <td>EQUINOX AWD 4C</td> <td>LT W/2LT</td> <td>BLACK</td> <td class="textCenter">S</td> <td class="textCenter">4</td> <td>GAS</td> <td class="textCenter">A</td> <td class="textCenter">AWD</td> <td class="textCenter">Y</td> <td>CD</td> <td class="textCenter">SR</td> <td class="textCenter">C</td> <td class="textRight" nowrap="nowrap">91,896</td> <td class="textRight" nowrap="nowrap">\$4,400</td> </tr> <tr><!-- 312 --> <td class="textCenter">2011</td> <td>CHEVROLET</td> <td>TAHOE 4X4 V8</td> <td>LTZ</td> <td>WHITE</td> <td class="textCenter">S</td> <td class="textCenter">8</td> <td>C</td> <td class="textCenter">A</td> <td class="textCenter">4X4</td> <td class="textCenter">A</td> <td>N</td> <td class="textCenter">MR</td> <td class="textCenter">L</td> <td class="textRight" nowrap="nowrap">126,982</td> <td class="textRight" nowrap="nowrap">\$17,800</td> </tr> <tr><!-- 313 --> <td class="textCenter">2011</td> <td>CHEVROLET</td> <td>1500 SLV 4X4 EX</td> <td>LT</td> <td>GRAY</td> <td class="textCenter">X</td> <td class="textCenter">8</td> <td>GAS</td> <td class="textCenter">O</td> <td class="textCenter">4X4</td> <td class="textCenter">A</td> <td>CD</td> <td class="textCenter">HT</td> <td class="textCenter">C</td> <td class="textRight" nowrap="nowrap">60,303</td> <td class="textRight" nowrap="nowrap">\$18,100</td> </tr> <tr><!-- 314 --> <td class="textCenter">2011</td> <td>CHEVROLET</td> <td>1500 SLV 4X4 EX</td> <td>LT</td> <td>SILVER</td> <td class="textCenter">X</td> <td class="textCenter">8</td> <td>E</td> <td class="textCenter">O</td> <td class="textCenter">4X4</td> <td class="textCenter">A</td> <td>CD</td> <td class="textCenter">HT</td> <td class="textCenter">C</td> <td class="textRight" nowrap="nowrap">89,403</td> <td class="textRight" nowrap="nowrap">\$15,900</td> </tr> <tr><!-- 315 --> <td class="textCenter">2011</td> <td>CHEVROLET</td> <td>1500 SLV 4X4 EX</td> <td>LTZ</td> <td>BLUE</td> <td class="textCenter">X</td> <td class="textCenter">8</td> <td>E</td> <td class="textCenter">A</td> <td class="textCenter">4X4</td> <td class="textCenter">A</td> <td>CD</td> <td class="textCenter">HT</td> <td class="textCenter">L</td> <td class="textRight" nowrap="nowrap">53,087</td> <td class="textRight" nowrap="nowrap">\$17,700</td> </tr> <tr><!-- 316 --> <td class="textCenter">2011</td> <td>CHEVROLET</td> <td>3500 CUTAWAY</td> <td>WORK VAN</td> <td>WHITE</td> <td class="textCenter">S</td> <td class="textCenter"></td> <td></td> <td class="textCenter">A</td> <td class="textCenter">4X2</td> <td class="textCenter"></td> <td>N</td> <td class="textCenter">HT</td> <td class="textCenter">C</td> <td class="textRight" nowrap="nowrap">202,477</td> <td class="textRight" nowrap="nowrap">\$2,700</td> </tr> <tr><!-- 317 --> <td class="textCenter">2011</td> <td>CHRYSLER</td> <td>TOWN &amp; COUNTRY</td> <td>TOURING</td> <td>BLACK</td> <td class="textCenter">4</td> <td class="textCenter">6</td> <td>E</td> <td class="textCenter">A</td> <td class="textCenter">4X2</td> <td class="textCenter">A</td> <td>CD</td> <td class="textCenter">HT</td> <td class="textCenter"></td> <td class="textRight" nowrap="nowrap">198,541</td> <td class="textRight" nowrap="nowrap">\$1,900</td> </tr> <tr><!-- 318 --> <td class="textCenter">2011</td> <td>DODGE</td> <td>DURANGO AWD V6</td> <td>CREW</td> <td>BLUE</td> <td class="textCenter">S</td> <td class="textCenter">6</td> <td>GAS</td> <td class="textCenter">A</td> <td class="textCenter">AWD</td> <td class="textCenter">A</td> <td>CD</td> <td class="textCenter">SR</td> <td class="textCenter">C</td> <td class="textRight" nowrap="nowrap">176,036</td> <td class="textRight" nowrap="nowrap">\$2,800</td> </tr> <tr><!-- 319 --> <td class="textCenter">2011</td> <td>FORD</td> <td>FOCUS</td> <td>SE</td> <td>SILVER</td> <td class="textCenter">4</td> <td class="textCenter">4</td> <td>GAS</td> <td class="textCenter">A</td> <td class="textCenter"></td> <td class="textCenter">Y</td> <td>CD</td> <td class="textCenter">HT</td> <td class="textCenter">C</td> <td class="textRight" nowrap="nowrap">101,929</td> <td class="textRight" nowrap="nowrap">\$3,100</td> </tr> <tr><!-- 320 --> <td class="textCenter">2011</td> <td>FORD</td> <td>FUSION FWD 4C</td> <td>SEL</td> <td>WHITE</td> <td class="textCenter">4</td> <td class="textCenter">4</td> <td>GAS</td> <td class="textCenter">A</td> <td class="textCenter"></td> <td class="textCenter">Y</td> <td>CD</td> <td class="textCenter">SR</td> <td class="textCenter">L</td> <td class="textRight" nowrap="nowrap">78,290</td> <td class="textRight" nowrap="nowrap">\$5,500</td> </tr> <tr><!-- 321 --> <td class="textCenter">2011</td> <td>FORD</td> <td>F150 4X4 CR</td> <td>XLT</td> <td>BLACK</td> <td class="textCenter">C</td> <td class="textCenter">8</td> <td>GAS</td> <td class="textCenter">A</td> <td class="textCenter">4X4</td> <td class="textCenter">Y</td> <td>CD</td> <td class="textCenter">HT</td> <td class="textCenter">C</td> <td class="textRight" nowrap="nowrap">70,909</td> <td class="textRight" nowrap="nowrap">\$16,000</td> </tr> <tr><!-- 322 --> <td class="textCenter">2011</td> <td>FORD</td> <td>MUSTANG V6 CPE</td> <td>V6 PREMIUM</td> <td>BLACK</td> <td class="textCenter">2</td> <td class="textCenter">6</td> <td>GAS</td> <td class="textCenter">A</td> <td class="textCenter">4X2</td> <td class="textCenter">A</td> <td>CD</td> <td class="textCenter">HT</td> <td class="textCenter">L</td> <td class="textRight" nowrap="nowrap">92,531</td> <td class="textRight" nowrap="nowrap">\$2,700</td> </tr> <tr><!-- 323 --> <td class="textCenter">2011</td> <td>GMC</td> <td>ACADIA FWD</td> <td>SLE</td> <td>RED</td> <td class="textCenter">4</td> <td class="textCenter">6</td> <td>GAS</td> <td class="textCenter">A</td> <td class="textCenter"></td> <td class="textCenter">A</td> <td>CD</td> <td class="textCenter">HT</td> <td class="textCenter">C</td> <td class="textRight" nowrap="nowrap">79,199</td> <td class="textRight" nowrap="nowrap">\$10,700</td> A picture of the table test data is attached here:

Here is the _query function, and all other relevant code that I can share.

If you know of a more efficient way of doing this please let me know would be more than happy to chew down my inefficiency while learning! (It's like my grandpa used to say, "The only criticism I can't use is that which is not given to me.")

The Item an I am using to test the filter is:

• By rm4453
Hello,

I am currently writing a program that parses a massive table from a website, and need a way to add a progress bar while parsing.
I am currently using the function _IETableWriteToArray(\$oObj, True) to parse the array. I need the progress bar to update as the table is parsed, not just at the end of the parsing.
Any help at all would be very much appreciated!

*EDIT --> The array I am left with after parsing is \$array[0-50000][16]
• By TrashBoat
So Im trying to make a simple 2d game and make some sort of collision detection so why not to make a 2 dimensional array but i have no clue how  to write it in multiple lines
Global \$map[5,5] = [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0] something like this but it doesn't work
• By Zein
#include "..\Include\Array.au3" #include "..\Include\File.au3" #include "..\Include\AutoItConstants.au3" Local \$aRetArray Local \$sFilePath = "n.csv" _FileReadToArray(\$sFilePath, \$aRetArray, ",") ; _FileReadToArray(\$sFilePath, \$aRetArray, \$FRTA_COUNT, ",") _ArrayDisplay(\$aRetArray, "Original", Default, 8) The above code shows two versions of _FileReadToArray and both don't work as expected.
The first one doesn't use the comma as a delimiter. (so I get a single column array)  I tried adding "Default" between \$aRetArray and "," then it told me it had an incorrect number of parameters.

I looked again at the documentation:

#include <File.au3> _FileReadToArray ( \$sFilePath, ByRef \$vReturn [, \$iFlags = \$FRTA_COUNT [, \$sDelimiter = ""]] )
And I with or without the flags params I should be getting a 2D array due to my file being a csv.
I then tried a regular flag, \$FRTA_COUNT, and it tells me that I'm using a variable \$FRTA_COUNT while it's not declared. Tried putting in 1 instead and it told me again, incorrect number of params.

• By ternal
Hi,
Recently I have had the need to do a sort and then do a second sort while the item of the first sort stays the same ( double sorting , first on column x then while column x is the same sort column y).
I did not put much efffort into error checking but so far I did not need it.
For my applications so far it works perfectly however if someone is willing I want to test this extensivly.
If anyone has big lists of random stuff to sort could you try this out please?
#include <Array.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _ArraySort_Double ; Description ...: ; Syntax ........: _ArraySort_Double (Byref \$array[, \$first_index = Default[, \$second_index = Default[, \$ascending = Default]]]) ; Parameters ....: \$array - 2d array to sort. ; \$first_index - [optional] first column to sort. Default is 0. ; \$second_index - [optional] second column to sort. Default is 1. ; \$ascending - [optional] ascending/descending. Default is 1. ; Return values .: 1 if no errors occured , -1 if errors occured ; Author ........: Ternal ; Remarks .......: Needs excessive testing. ; Related .......: _arraysort() ; =============================================================================================================================== Func _ArraySort_Double (byref \$array, \$first_index = Default, \$second_index = Default, \$ascending = Default) Local \$temp_value Local \$counter = 1 If UBound(\$array, \$UBOUND_DIMENSIONS) <> 2 Then MsgBox(0, "error", "error") return -1 EndIf If \$first_index = Default Then \$first_index = 0 If \$second_index = Default Then \$second_index = 1 If \$ascending = Default Then \$ascending = 1 _ArraySort(\$array, \$ascending, 0, 0, \$first_index); you can alter settings of primary sort here If @error Then MsgBox(0, "error", @error) return -1 EndIf \$temp_value = \$array[0][\$first_index] For \$x = 1 to UBound(\$array, 1) - 1 If Mod( \$x, 10000) = 0 Then ConsoleWrite("at " & \$x & " of a total : " & UBound(\$array, 1) & @CRLF) If \$array[\$x][\$first_index] = \$temp_value Then \$counter+= 1 If \$x = UBound(\$array, 1) - 1 Then; do last line here(if last line is not a new item) _ArraySort(\$array, \$ascending, \$x - \$counter, \$x, \$second_index);you can alter settings of secondary sort here(don't forget to place line 34 the exact same) If @error Then MsgBox(0, "error", @error) return -1 EndIf EndIf Else If \$counter > 0 Then ;at least 2 of the same _ArraySort(\$array, \$ascending, \$x - \$counter, \$x - 1, \$second_index);you can alter settings of secondary sort here(don't forget to place line 29 the exact same) If @error Then MsgBox(0, "error", @error) return -1 EndIf \$counter = 1 EndIf EndIf \$temp_value = \$array[\$x][\$first_index] Next Return 1 EndFunc Kind regards, Ternal
×

• Wiki