Solved

How to search an Array for a Text value

Posted on 2006-07-09
7
316 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 85

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

825 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