Solved

Find index of TList item

Posted on 2002-04-23
9
431 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6964634
try to use TStringList
0
 

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 25

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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