Solved

Find index of TList item

Posted on 2002-04-23
9
347 Views
Last Modified: 2010-04-04
Hi...
   I've created a TList that looks like this:

Type
  AList = record
    cc_name : string;
    cc_count : longint;
  end;

  PMyList = ^AList;

Var
  ccList : TList;
  ccRec : PMyList;


I populate this list with records, then later I want to find the index in this list of the item with cc_name equal to for instance, "BOB". How would I do this?

Thanks
   Shawn
0
Comment
Question by:aztec
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 6964440
simply like :

with ccList do
  for I:=0 to Count-1 do
    if PMyList(Items[I])^.cc_name = SearchString then
    begin
      Result:=I;// or whatever you do ...
      break;
    end;
0
 

Author Comment

by:aztec
ID: 6964493
ok, but what about using the "IndexOf" thing? Won't that work somehow?

Shawn
0
 

Expert Comment

by:lottol
ID: 6964630
You can only compare pointers to your items(vars) by "IndexOf".
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6964634
try to use TStringList
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:aztec
ID: 6964726
DragonSlayer - can I still add records of the type I mention above (a string item and a longint item) to a TStringList? And I'll still be able to use the IndexOf method?
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6964805
You can, for example, do this

var
  MyStringList: TStringList;

in your OnCreate:

MyStringList := TStringList.Create;

and in wherever your data retrieval algorithm is:

var
  ccRec: PMyList;
  i: Integer;
begin
  // let's say you have a bunch of data
  for i := 0 to DataCount - 1 do
  begin
    New(ccRec);
    ccRec^.cc_name = ReadData; // assuming this is your data retrieval function
    ccRec^.cc_count = ReadCount; // see comment as above
    MyStringList.AddObject(ccRec^.cc_name, TObject(ccRec));
  end;
end;

and in your OnDestroy

for i := MyStringList.Count - 1 downto 0 do
  Dispose(PMyList(MyStringList.Objects[i]));
MyStringList.Free;


and now to retrieve:

function Retrieve(const cName: string): AList;
var
  i: Integer;
begin
  i := MyStringList.IndexOf(cName);
  if i < 0 then
    ShowMessage('Error') // or whatever error msg/default values you want to return
  else
    Result := PMyList(MyStringList.Objects[i])^;
end;



HTH
DragonSlayer.
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 6964988
but with a stringlist you have double lists
I like to keep code optimized as possible
as for speed as for memory
depends what you're doing anyway
0
 
LVL 24

Expert Comment

by:Tomas Helgi Johannsson
ID: 6965144
Hi!

You could override the TList and impliment a Find function
similar to TListSortCompare(...). Look at the code for the
TList class how it is done.

Regards,
Tomas Helgi
0
 
LVL 8

Accepted Solution

by:
TOndrej earned 50 total points
ID: 6965276
If all you want to store is a string and its associated integer, you don't need to declare a new record type. Your easiest option is to use TStringList with AddObject:

var
  List: TStringList;
  I: Integer;
begin
  List := TStringList.Create;
  try
    // adding strings with integers
    List.AddObject('some string', TObject(215));
    List.AddObject('some other string', TObject(1057));

    // retrieving strings and integers
    for I := 0 to List.Count - 1 do
      ShowMessage(Format('Item %d: ''%s'' %d',
        [I, List[I], Integer(List.Objects[I])]));
  finally
    List.Free;
  end;
end;

You can then simply use IndexOf (or Find, if your list is Sorted) to search for a string.

HTH
TOndrej
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 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

14 Experts available now in Live!

Get 1:1 Help Now