Jump to content



Photo

_arraySearch2d()


  • Please log in to reply
3 replies to this topic

#1 ChrisL

ChrisL

    Mass Spanner!

  • Active Members
  • PipPipPipPipPipPip
  • 1,746 posts

Posted 16 November 2007 - 09:48 PM

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

Plain Text         
#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

Attached Files


Edited by ChrisL, 18 November 2007 - 09:28 PM.






#2 weaponx

weaponx

    I'm coming for blood, no code of conduct, no law.

  • MVPs
  • 5,366 posts

Posted 17 November 2007 - 12:28 AM

There already exists a function that can search an array of any dimension:
http://www.autoitscript.com/forum/index.php?showtopic=35441

#3 ChrisL

ChrisL

    Mass Spanner!

  • Active Members
  • PipPipPipPipPipPip
  • 1,746 posts

Posted 17 November 2007 - 02:15 AM

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, 17 November 2007 - 02:20 AM.


#4 Champak

Champak

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 882 posts

Posted 17 February 2008 - 07:30 AM

Just wanted to chime in, very nice. One of the easiest examples to quickly learn and adapt.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users