Solved

Find index of TList item

Posted on 2002-04-23
9
456 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

752 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