[fpc-pascal] A complex trouble(at least for me)

Martin Friebe fpc at mfriebe.de
Fri May 8 12:17:02 CEST 2009


ArĂ­ Ricardo Ody wrote:
> Suppose the following structure:
>
> 01 a-1.
>    03 b-1 pic xxx.
>    03 b-2 pic 999.
>    03 b-3.
>       05 c-1 pic aaa.
>       05 c-2.
>          07 d-1 pic zzz.
>          07 d-2 pic xxx.
>       05 c-3 pic 99.
>    03 b-4 pic zzz.
>    03 b-5.
>       05 e-1 pic zzz.
>
> All line not containing the word pic I would call a group item and the 
> other lines will be elementary items.
>
> I would like to wrote a routine(or program) that save each group item 
> and the elementary items contained in a StringList. Admit that the 
> pattern of the structure is random. The example above is only an example.
>
> In this example:
> StringList1 will contain 'a-1' '03 b-1 pic xxx.' '03 b-2 pic 999.' '03 
> b-4 pic zzz.'
> StringList2 will contain 'b-3' '05 c-1 pic aaa.' '05 c-3 pic 99.'
> StringList3 will contain 'c-2' '07 d-1 pic zzz.' '07 d-2 pic xxx.'
> StringList4 will contain 'b-5' '05 e-1 pic zzz.'
>
> At the end of the program I will(for example) write the StringList's 
> to disk. But I can't write anyone before the program end.
>
> I had thinked hard about it. I can't create StringList with a variable 
> name. I must previously  declare each one in my pascal source. How can 
> I solve the trouble proposed?
"I can't create StringList with a variable name."

I don't know how much pascal experience you have. and I am not 100% sure 
what this line means, but taking a guess:
You need a way to store "stringlist2" in stringlist1 => So that if you 
later read the list, you can read the nested lists too?

You do not need a variable for each Stringlist you create.

Procedure AddNestedStringlist(OuterStringlist: TStringList; Text: String);
var
  InnerStringList: TStringlist;
begin
  InnerStringList := TStringlist.Create;
  OuterStringlist.AddObject(Text, InnerStringlist);
end;

Thats it. Note that there is no InnerStringlist.Free.


The stringlist you created is not bound to the variable 
"InnerStringlist". It is just a stringlist in memory, and any variable 
(of the correct type) can refer to it.

To read the variable back (you need a type cast):
NextedStringList := TStringList(OuterStringList.Objects[n]);


Before you Free OuterStringlist, you have to check for any 
InnerStringlist and destroy it, or you will leak memory.

>
> Note that when I'm appending strings in a StringList, it's possible 
> that I must create a new StringList(or various), put data in it(or 
> them) and return to put data in the first StringList.
>
> If the solution is obvious I beg your pardon. I can't find it. * *
> ------------------------------------------------------------------------
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal



More information about the fpc-pascal mailing list