Solved

Trying to find an element in a string array

Posted on 1998-08-07
9
119 Views
Last Modified: 2010-04-04
Hi...
  I need to be able to search for a string in a string array of 250 different constants. Basically I read a line from a file,  then go thru a for loop 250 times checking against each element of the array to see if the line matches one of the strings in there. Then I read in the next line from the file and do the whole process again. This takes a really long time for it to run. Is there a quicker, more efficient way?

thanks,
   Shawn Halfpenny
   drumme59@sprint.ca
0
Comment
Question by:aztec
9 Comments
 
LVL 5

Expert Comment

by:inter
Comment Utility
Hi,

I am happy that somebody up there(my local time is 9:28 p.m). And the answer is YES. We can make the complexity of your search in the order of O(log2(N)) with binary search as follows:
- Insert all your 250 strings to a TStringList and set sorted property to true
- Read aline from file and use( say you have StList)
  if StList.Find(MyCnstReadFromFile, Index) then
  begin
     // you have found it at Index'th item in the list
  end;

Regards, (need full source, just give me couple of examples about your constants but it may say several minutes)
Igor
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
inter,

does TStringlist uses smart search?

Zif.
0
 
LVL 5

Expert Comment

by:inter
Comment Utility
Let me see...here in few moments...(I suppose binarry search is embeded)
0
 
LVL 5

Expert Comment

by:inter
Comment Utility
Yes, It sorts with quicksort and searches with binary search. It is fast, so lets calculate,

if the set has 250 elements one should search all in normal search which has
250 ops
binary search does it in
log2(250) = log10(250)/log10(2)=7.97 => 8 ops
so there is about 250/8 = 31.25 speed up.
So in plain if it does in 100 secs
in fast mode it does it in 3.2 secs (theoretically)
Regards,
Igor
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
well Igor, you did it again.
0
 

Author Comment

by:aztec
Comment Utility
Indeed, as Zifnab put it Igor, you have done it again! Works great and very fast too! Give yourself an 'A' on this one.

Thanks
   Shawn Halfpenny
0
 

Expert Comment

by:DPedrelli
Comment Utility
If your array is sorted then you could do a Btrieve style search.  Your value is either < or > the value of the current element.  Temp[0] < Value then jump half way.  If Temp[125] > Value then jump back half way.  If Temp[75] < Value ... and so on.
If you don't require an array, you could use a TStringList which gives you two easy methods.  TStringList.IndexOf() or TStringList.Find().

D.
0
 
LVL 5

Expert Comment

by:inter
Comment Utility
Hi, thanks,
Am I supposed to post an answer?
0
 
LVL 5

Accepted Solution

by:
inter earned 50 total points
Comment Utility
Hi,
Just scanning all the questions and found this, he he ;-)
Regards, Igor
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now