WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Arrays
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Searching an array for data


Question:

What is the most efficient way to search an array for data without knowing element positions? I have a 2 dimensional array (filled from a CSV file) that I need to find if dimension 1 matches inputted data from a dialog box, and if so get the data that corresponds in dimension 2.

I currently use For Next nested loops and equality checks but this takes far too long, especially if the data being searched is not present.

Answer:

For current versions of WinBatch use ArrayLocate or ArraySearch.

ArrayLocate

strList = 'a b c A B C 1 0 -1'
value = 'C' ; (uppercase letter C)
sub1 = 2 ; Start on the third element of the first dimension
; Create an array to search
arrList = Arrayize( strList, ' ' )
; Search the array starting on the third element of the first dimension
arrRslt = ArrayLocate(arrList,value,sub1)
; Read result from the array
result = arrRslt[0]
; Display results ( expect 5 )
Pause( 'ArrayLocate of value: ' : value, 'Element number: ' : result )
Exit
; find all occurrences of "fred" in a two dimensional array
arr = ArrDimension(3,2)
arr[0,0] = "fred"
arr[0,1] = "fred"
arr[1,0] = "john"
arr[1,1] = "bill"
arr[2,0] = "tom"
arr[2,1] = "fred"
res = ArrDimension(2)
res[0] = 0
res[1] = 0
While 1
   res = ArrayLocate(arr,"fred",res[0],res[1])
   If res[0] == -1 Then Break
   Message(res[0],res[1])
   res[1] = res[1] + 1 ; skip that one
EndWhile 
; To find name and city for particular id.
array = ArrDimension (25, 3)
array[1, 0]  = "WN001"     ; id
array[1, 1]  = "fred"      ; name
array[1, 2]  = "amsterdam" ; city
array[2, 0]  = "WN002"     ; id
array[2, 1]  = "pete"      ; name
array[2, 2]  = "rotterdam" ; city
array[7, 0]  = "WN007"     ; id
array[7, 1]  = "detlev"    ; name
array[7, 2]  = "wuppertal" ; city
array[24, 0] = "WN024"     ; id
array[24, 1] = "john"      ; name
array[24, 2] = "new york"  ; city
lookfor = "WN007"
arrResult = ArrayLocate (array, lookfor ) ; arrResult = [7,0]
id = array [arrResult[0], arrResult[1]]
name = array [arrResult[0], arrResult[1] + 1]
city = array [arrResult[0], arrResult[1] + 2]
Pause( 'Results', id: @LF : name : @LF : city )

ArraySearch

strList = 'a b c A B C 1 0 -1'
value = 'C' ; (uppercase letter C)
; Create an array to search
arrList = Arrayize( strList, ' ' )
; Search the array
result = ArraySearch( arrList, value, 0, 0, 0, ArrInfo(arrList,1)-1)
; Display results ( expect 5 )
Pause( 'ArraySearch of value: ' : value, 'Element number: ' : result )
Exit
When choosing the Binary Fast Search options 1 & 3 to find every occurrence of a duplicate value, you must search in both directions from the first found item. It makes more sense to just do a Linear search when you want to find every occurrence of a value unless you have a very large data set.

The following paragraph is taken from the ArraySearch functions documentation.

Duplicates
 This function will not necessarily locate the item with the lowest index of a set of duplicates. To find every occurrence of a duplicate value, you must search in both directions from the first found item. A linear search is the preferred method to find every occurrence of a value, unless you have a very large data set. 

Fast Binary Search Options 1 & 3
 This function can optionally search using a fast binary search but the array must first be sorted on "search-column' using the ArraySort function. 

Linear Search Options 0 & 2
 A sequential search method for finding a value in an array. Checking each of its elements in sequence, until the desired one is found. If your intent is to locate all duplicate items then a linear search is recommended. 


OLD RESPONSE

What you want is "Content Addressable Memory"

where you can lookup the #2 data by supplying the #1 data without the tedious searching.

Use the Content Address memory UDF
http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/UDF~-~UDS~Library+Content~Addressable~Memory.txt

Basically copy and paste the whole bit of code and then just figure outthe very bottom part. All the UDF stuff will work like magic.

If there is a delay, it will be in the loading of the data on initialization.

If the data never changes, there are some sleazy hacks available.


OR get the CAM extender: http://hpdd.de/hpdd/htm/dd383000.htm#WBExt007


Article ID:   W15891
File Created: 2014:07:18:09:50:34
Last Updated: 2014:07:18:09:50:34