I.T. Discussion Community!
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

December Edition
Subscribe now! It's Free!
Enter your email:

   ► MB LobbyPascal and Delphi Coding BoardDelphi News / Other Topic   Print This     

Lookup a value in an array

Lookup a value in an array in Delphi News / Other topic (part of our Pascal and Delphi Coding group).

Quick Search: Lookup value   Lookup value array  
Matt
Canada
Is there a more efficient way to look up a value in an array then this:

for i := 0 to length(myArr) - 1 do
begin
if myArr := myValue then
found := true ;
end ;

something like
if myValue in myArr then
found := true ;

probably a stupid question, but I'm only using Delphi occasionally (though I love it).

TIA,
Matt
 Posted 15 years ago (Thread Starter)
Comment Quote
About Matt -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.

Post ID #10406, 1 replies
Thread Started 3/16/2004 5:30:00 PM
View Counter=8176
Last Reply Posted 3/25/2004 1:11:00 PM)
Location=Canada  
Joined=18 years ago   MB Posts=14  
Most Recent Post
Rick Carter
Cincinnati OH USA
If you're working with arrays of strings, what I would do is use a TStringList, which has a Find() method.

I don't know how much it will relate to what you're trying to do, but here's a Pascal unit I wrote that allows you to mimic the behavior of TDataSet.FindNearest with any list of strings:
unit UnitStringListUtil;

interface

uses
Classes;

function FindNearestIndex(s : String; strings : TStrings): Integer;

function FindNearestString(s : String; strings : TStrings): String;

implementation

{Use to find index in a sorted list.}
function FindNearestIndex(s : String; strings : TStrings): Integer;
var
StringList : TStringList;
i, iCount : integer;
begin
StringList := TStringList.Create;
try
StringList.Assign(strings);
StringList.Sorted := True;
StringList.Find(s, i);
iCount := StringList.Count - 1;
if i > iCount then
Result := iCount
else
Result := i;
finally
StringList.Free;
end;
end;

{Use with an unsorted list.}
function FindNearestString(s : String; strings : TStrings): String;
var
StringList : TStringList;
i, iCount : integer;
begin
StringList := TStringList.Create;
try
StringList.Assign(strings);
StringList.Sorted := True;
StringList.Find(s, i);
iCount := StringList.Count - 1;
if i > iCount then
i := iCount;
Result := StringList.Strings;
finally
StringList.Free;
end;
end;

end.
Rick Carter
Chair, Delphi/Paradox SIG
Cincinnati PC Users Group
 Posted 15 years ago
Comment Quote
About Rick Carter -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.
Old Account!
If this is your account, sign in to activate web presence data (sign in quarterly to keep active). Alternatively, you can subscribe to our monthly eMag with a valid email address.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #10416 (Level 1.1)  Reply to 10406
Thread Started 3/25/2004 1:11:00 PM
View Counter=2
Location=Cincinnati OH USA  
Joined=18 years ago   MB Posts=518  

Revive Thread!

Add a comment to revive this old thread and make this archived thread more useful.

Write a Comment...
Full Editor
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P192A1
Enter key:
Icon: A Post    Thread    Idea    Important!    Cool    Sad    No    Yes    Includes a Link...   
Thread #10406 Counter
8176
Since 4/2/2008

Follow PrestwoodBoards on: 


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech