Solved

How to search an Array for a Text value

Posted on 2006-07-09
7
318 Views
Last Modified: 2010-04-23
Quick question.  What is the best approach to search an array for a text string string.  I have 2 array's.  I loop through an array filenames and for each file name I want to see if it exists on another array.  The second array only has four columns and the column I need to search is the 4th column.  Very simple code as follows:

'Array 1
Dim fileEntries() As String = System.IO.Directory.GetFiles(TextBox1.Text)
Dim filename As String
'Array 2
Dim Fields() As String
Fields = Str.Split(","c)
For Each filename In fileEntries

    ??? What is the best way to search the Fields(3) array column 4 using filename.

Next  
     
0
Comment
Question by:Hojoformo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 17

Accepted Solution

by:
ZeonFlash earned 500 total points
ID: 17068936
You can try using the BinarySearch method on your array, but since I don't have my dev machine in front of me, I can't test it.  I'm 99% sure that you can search for strings using it.

-----
'Sort the array first, since a binary search performs optimally on a sorted list
Array.Sort(Fields(3))
Dim index As Integer

For Each filename In fileEntries
     index = Array.BinarySearch(Fields(3), filename)
Next  
------

As I said, I can't test any of that out, but it might get you started in the right direction.
0
 
LVL 14

Expert Comment

by:Ramuncikas
ID: 17068946
'Array 1
Dim fileEntries() As String = System.IO.Directory.GetFiles(TextBox1.Text)
Dim filename As String
'Array 2
Dim Fields() As String
Fields = Str.Split(","c)
For Each filename In fileEntries
   If filename = Fields(3) Then
      'I found it
   End If
Next  
     
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17069046
The "best" way is to use a different structure, such as a Collection or a HashTable.

Bob
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17069662
I agree with Bob...

A Binary Search is only better than an iterative (For...Next) search if the items in the array are ALREADY sorted.
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17075405
HashTable (or System.Collections.Generic.Dictionary) are nice, since you can use the .ContainsKey(keyName) to determine if the key already exists -- without searching.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17078413
Big-O:  O(1) ;)  Thanks Jeff.

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17142892
Sometimes people just don't listen (boo-hoo)!!!

Bob
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question