Solved

How to search an Array for a Text value

Posted on 2006-07-09
7
319 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

635 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