Jump to content

_arraySearch2d()


ChrisL
 Share

Recommended Posts

I needed to search a 2d array and the _arraySearch() function only supports a 1d array

So I wrote this based on the standard 1d arraySearch()

###User Defined Function###

_ArraySearch2d

###Description###

Finds an entry within a Two-dimensional array.

###Syntax###

_ArraySearch2d($avArray, $vWhat2Find, $i_RowStart = 0, $i_RowEnd = 0, $i_ColumnStart = -1, $i_ColumnEnd = -1, $iCaseSense = 0,

$fPartialSearch = False)

###Parameters###

@@ParamTable@@

$avArray = The array to search

$vWhatToFind = What to search $avArray for

$i_RowStart (Optional) = Start array index for search, normally set to 0 or 1. If omitted it is set to 0

$i_RowEnd (Optional) = End array index for search. If omitted or set to 0 it is set to Ubound($avArray)-

$i_ColumnStart (Optional) = First Column to start the search from Default value is -1 start at 0 column

$i_ColumnEnd (Optional) = Last Column to allow in the search Default value is -1 search all columns

$iCaseSense (Optional) = If set to 1 then search is case sensitive

$fPartialSearch (Optional) = If set to True then executes a partial search. If omitted it is set to False

@@End@@

###ReturnValue###

@@ReturnTable@@

On Success - Returns an array of the positions of the item in a 2darray. $ret[0] = the Row number $ret[1] = the Column number

On Failure - Returns -1 and sets @Error on Errors.

@Error=1 $avArray is not an array

@Error=2 $avArray is a 1dimensional not a 2d array

@Error=3 $i_RowEnd is greater than UBound($avArray)-1

@Error=4 $i_RowEnd is less than 0

@Error=5 $i_ColumnStart is greater than UBound($avArray,2)-1 (the amount of columns)

@Error=6 $i_ColumnStart is less than 0 (columns)

@Error=7 User $i_RowStart value is greater than the $i_RowEnd value

@Error=8 User $i_ColumnStart value is greater than the $i_ColumnEnd value

@Error=9 User specified $i_ColumnStart value is greater than UBound($avArray,2)-1 (the amount of columns)

@Error=10 $iCaseSense was invalid. (Must be 0 or 1)

@Error=11 $vWhatToFind was not found in $avArray

@@End@@

###Remarks###

None.

Example use below

#include <array.au3>
#include <array2.au3>

;Example

Local $my2dArray[5][5]
$my2dArray[0][0] = "Name"
$my2dArray[0][1] = "Points"
$my2dArray[0][2] = "Town"
$my2dArray[0][3] = "Colour"
$my2dArray[0][4] = "Manufacturer"

$my2dArray[1][0] = "Peter"
$my2dArray[1][1] = "12"
$my2dArray[1][2] = "Peterborough"
$my2dArray[1][3] = "Green"
$my2dArray[1][4] = "Vauxhall"

$my2dArray[2][0] = "Alan"
$my2dArray[2][1] = "23"
$my2dArray[2][2] = "Stamford"
$my2dArray[2][3] = "Blue"
$my2dArray[2][4] = "Ford"

$my2dArray[3][0] = "Sarah"
$my2dArray[3][1] = "30"
$my2dArray[3][2] = "Cambridge"
$my2dArray[3][3] = "Yellow"
$my2dArray[3][4] = "Volkswagen"

$my2dArray[4][0] = "Jane"
$my2dArray[4][1] = "52"
$my2dArray[4][2] = "Northampton"
$my2dArray[4][3] = "Pink"
$my2dArray[4][4] = "Fiat"

_ArrayDisplay($my2dArray, "Visual representation of $my2dArray")

$what2Find = "Volkswagen"
$pos = $pos = _ArraySearch2d($my2dArray, $what2Find, 0, 0, -1, -1, 0, False)
If Not @error Then
    MsgBox(0, "", $what2Find & " is found in row " & $pos[0] & " Column " & $pos[1] & @CRLF & _
            "$my2dArray[" & $pos[0] & "][" & $pos[1] & "] = " & $what2Find)
Else
    MsgBox(0, "", "Error " & @error)
EndIf

Edit: Edited with standard variable names

array2.au3

Edited by ChrisL
Link to comment
Share on other sites

There already exists a function that can search an array of any dimension:

http://www.autoitscript.com/forum/index.php?showtopic=35441

Well I did search for an _arraySearch2d before I started but didn't see one, admittedly I didn't search for multidimensional however just looking at that one I think you need to be quite specific about wich column your interested in

In mine you don't have to be specific at all unless you want to.

If you look at my example I search for Volkswagen but I don't specify which column it has to be in but I could if I wanted too!

It has the same case sensitive match and partial match like the standard _arraySearch() the same only search between specified rows but also columns too

Edited by ChrisL
Link to comment
Share on other sites

  • 3 months later...

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...