Solved

Find index of TList item

Posted on 2002-04-23
9
386 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 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