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.

ArraySearch Duplicate Values

 Keywords: ArraySearch Binary Fast Search array Duplicate Multiple Values Option 1 3 

Question:

strList = 'a a b c A A B C C 1 0 -1 -1'
value = 'C'
; Create an array to search
arrList = Arrayize (strList, ' ')
; Sort the array
ArraySort (arrList)
endsub = ArrInfo (arrList, 1) - 1
; Search the array
startsub = 0
result = 0
While @TRUE
   result = Arraysearch (arrList, value, 3, 0, startsub, endsub)
   If result == -1 Then Break
   Pause ('ArraySearch of value: ' : value, 'Element number: ' : result)
   startsub = result + 1
   If startsub > endsub Then Break
EndWhile
Exit
When using a binary search, there is a problem to find the small "c"? Is this a bug?

Answer:

This is not considered a bug. This is actually how the function is designed and is expected behavior. 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.


Article ID:   W17658
Filename:   ArraySearch Duplicate Values.txt
File Created: 2011:07:07:08:58:20
Last Updated: 2011:07:07:08:58:20