Board index » delphi » Another Stringlist {*word*218} question ?

Another Stringlist {*word*218} question ?

Hello for the second time today,

I have 6 stringlists :

SL1
SL2
SL3
SL4
SL5
SL6

and an integer - 'i'

What is the best way to do something like this which gets the 1st item
in list for all 6 stringlists, without me having to specifically go
after each SL separately.

        For i := 1 to 6 do

        begin

        SL+(i)[0];

        //get 1st item in list for all 6 stringlists
        //...do other stuff with em

        end;

and so on. Thanks.
Liam Healy

 

Re:Another Stringlist {*word*218} question ?


Well, you could put the six lists onto a TList.  Here's one way to do it:

        myList.Add(SL1);
        myList.Add(SL2);
        ...
{ assumes SL1, SL2, et al refer to already-existing stringlists. }

then:   (myList[0] as TStringList)[4]
will return the fourth entry from the zeroth (SL1) stringlist.  

[Naturally I'd probably create a class which encapsulates that logic and
simply presents me with "a two dimensional array of strings."  That way I
don't have messy statements like that all over my application.]

Remember that "objects are pointers," so when I "add SL1 to the list," I now
have two copies of that pointer, and they point to one object.  They're now
synonyms.  I don't have to worry about bothering them when I discard
"myList" or do anything else to it.  (But you do still have to free them
somewhen.)

Another way I could do the same thing is to create the TStringLists purely
on-the-fly:

        for n := 0 to 5 do myList.Add(TStringlist.Create);

Now I've created six instances of a TStringList and have put them onto the
list.  Before I get rid of myList then I need to:

        for n := 0 to 5 do (myList[n] as TStringList).Free;
        myList.Clear;   {or myList.Free}

The two statements should be adjacent because when I free object "n" the
now-garbage pointer to it remains in the list.  But I clear or free the
list itself in the next statement.

Quote
>L. Healy wrote:
> Hello for the second time today,

> I have 6 stringlists :

> SL1
> SL2
> SL3
> SL4
> SL5
> SL6

> and an integer - 'i'

> What is the best way to do something like this which gets the 1st item
> in list for all 6 stringlists, without me having to specifically go
> after each SL separately.

>         For i := 1 to 6 do

>         begin

>         SL+(i)[0];

>         //get 1st item in list for all 6 stringlists
>         //...do other stuff with em

>         end;

----------------------------------
Fast automatic Paradox table repair at a click of a mouse!
http://www.sundialservices.com/products/chimneysweep

Re:Another Stringlist {*word*218} question ?


On Wed, 16 Oct 2002 15:33:29 +0100, "L. Healy"

Quote
<L.He...@occpsy.demon.co.uk> wrote:
>I have 6 stringlists :

>and an integer - 'i'

>What is the best way to do something like this which gets the 1st item
>in list for all 6 stringlists, without me having to specifically go
>after each SL separately.

>        For i := 1 to 6 do

>        begin

>        SL+(i)[0];

>        //get 1st item in list for all 6 stringlists
>        //...do other stuff with em

>        end;

As Sundial said, I'd probably go with a TList. But, you can use an
array too, which is probably simpler if you don't need a dynamcally
changing number of TStringLists...

var
  i : integer;
  SLA : array[0..5] of TStringList;
begin
  for i := 0 to 5 do
    SLA[i] := TStringList.Create;

  //... populate lists ...

  for i := 0 to 5 do
    ShowMessage(SLA[i][0]);
end;

--
jc

Re:Another Stringlist {*word*218} question ?


Quote
"L. Healy" <L.He...@occpsy.demon.co.uk> wrote:
>Hello for the second time today,

>I have 6 stringlists :

>SL1
>SL2
>SL3
>SL4
>SL5
>SL6

>and an integer - 'i'

>What is the best way to do something like this which gets the 1st item
>in list for all 6 stringlists, without me having to specifically go
>after each SL separately.

>        For i := 1 to 6 do

>        begin

>        SL+(i)[0];

>        //get 1st item in list for all 6 stringlists
>        //...do other stuff with em

>        end;

>and so on. Thanks.
>Liam Healy

You couldn't do something like SL+(i)[0] as you say above, because
these are 6 individual class instances NOT a multi-dimentional array.

If there were only 6, maybe it'd be clearer just to say something
like:

ShowMessage(SL1[0]);
ShowMessage(SL2[0]);
{etc.}

But if there were more, or you don't like this solution you could
maybe make an array of string lists, rather than 6 individual ones,
and do something like this:

// StringListArray is declared: array [1..6] of tStringList

procedure SomeProc;
var I: Integer;
begin
    for I := 1 to 6 do
        ShowMessage(StringListArray[I].Strings[0]);
end;

HTH,

Tom

--
Visit the link to get paid for reading e-mail (it's what I do!):
http://www.zwallet.com/index.html?user=tommyboytjb

Re:Another Stringlist {*word*218} question ?


Hi,
this won't work because you can't create names for variables at runtime. I
think the best you can do is put the stringlists in an array, like this:

var
  StrLists : array [0..5] of TStringList;

Then you can use the index to access the individual stringlists.

-------------------------
http://users.pandora.be/koen.van.baelen
-------------------------

"L. Healy" <L.He...@occpsy.demon.co.uk> schreef in bericht
news:AZXjALA5iXr9EwlX@occpsy.demon.co.uk...

Quote
> Hello for the second time today,

> I have 6 stringlists :

> SL1
> SL2
> SL3
> SL4
> SL5
> SL6

> and an integer - 'i'

> What is the best way to do something like this which gets the 1st item
> in list for all 6 stringlists, without me having to specifically go
> after each SL separately.

>         For i := 1 to 6 do

>         begin

>         SL+(i)[0];

>         //get 1st item in list for all 6 stringlists
>         //...do other stuff with em

>         end;

> and so on. Thanks.
> Liam Healy

Re:Another Stringlist {*word*218} question ?


In article <AZXjALA5iXr9E...@occpsy.demon.co.uk>, "L. Healy"

Quote
<L.He...@occpsy.demon.co.uk> writes:
>Hello for the second time today,

>I have 6 stringlists :

>SL1
>SL2
>SL3
>SL4
>SL5
>SL6

>and an integer - 'i'

>What is the best way to do something like this which gets the 1st item
>in list for all 6 stringlists, without me having to specifically go
>after each SL separately.

You _could_ use a TStringGrid with Visible set to false. The Rows[n] of a
stringgrid is a TStrings of the cell contents of row n. And the Cols[n] is a
TStrings of the cell contents of column n.

Personally I'd rather code a TList of TStringLists, or an array of
TStringLists.

Alan Lloyd
alangll...@aol.com

Other Threads