Recent

Author Topic: [SOLVED] Adding stringlists optimized  (Read 2946 times)

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
[SOLVED] Adding stringlists optimized
« on: July 19, 2018, 09:21:25 pm »
I have 2 StrinLigs, lets say with the following strings:

(length is unknown)
Old (A,B,C...)
New (D,E,F,G...)

What i want is this : New (A,B,C...,D,E,F,G...) (New := Old+New;// do not works)

Thanks
« Last Edit: July 26, 2018, 07:27:28 pm by torbente »
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Adding stringlists optimized
« Reply #1 on: July 19, 2018, 09:34:34 pm »
AddStrings

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Adding stringlists optimized
« Reply #2 on: July 19, 2018, 09:35:56 pm »
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
  2. uses classes;
  3. const
  4.   ca:array[0..2] of string = ('a','b','c');
  5.   cb:array[0..2] of string = ('d','e','f');
  6.  
  7. var a,b:Tstrings;
  8. begin
  9.   a:=Tstringlist.create;
  10.   b:=Tstringlist.create;
  11.   a.Addstrings(ca);
  12.   b.AddStrings(cb);
  13.   a.addstrings(b);
  14.   writeln(a.text);
  15.   b.free;
  16.   a.free;  
  17. end.
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Adding stringlists optimized
« Reply #3 on: July 19, 2018, 09:37:35 pm »
AddStrings
Yup. Our posts crossed. Added very simple example.
Specialize a type, not a var.

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Adding stringlists optimized
« Reply #4 on: July 19, 2018, 10:03:52 pm »
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
  2. uses classes;
  3. const
  4.   ca:array[0..2] of string = ('a','b','c');
  5.   cb:array[0..2] of string = ('d','e','f');
  6.  
  7. var a,b:Tstrings;
  8. begin
  9.   a:=Tstringlist.create;
  10.   b:=Tstringlist.create;
  11.   a.Addstrings(ca);
  12.   b.AddStrings(cb);
  13.   a.addstrings(b);
  14.   writeln(a.text);
  15.   b.free;
  16.   a.free;  
  17. end.
Why this function is not working?

Code: Pascal  [Select][+][-]
  1. Function JoinSLs(old,new:TStringlist):Stringlist;
  2. Begin
  3. Result := Old.AddStrings(new);
  4. End;
  5.  
  6. New := JoinSLs(Old,New); // wrong values

blocks.pas(305,12) Error: Invalid assignment, procedures return no value
« Last Edit: July 19, 2018, 10:09:24 pm by torbente »
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Adding stringlists optimized
« Reply #5 on: July 19, 2018, 10:11:45 pm »
Because Addstrings is a procedure, not a function....
https://www.freepascal.org/docs-html/rtl/classes/tstrings.addstrings.html
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
  2. uses classes;
  3. const
  4.   ca:array[0..2] of string = ('a','b','c');
  5.   cb:array[0..2] of string = ('d','e','f');
  6.  
  7.   Function JoinSLs(const old,new:TStrings):TStrings;
  8.   Begin
  9.     Result := Old;
  10.     Result.AddStrings(new);
  11.   End;
  12.    
  13. var a,b:Tstrings;
  14. begin
  15.   a:=Tstringlist.create;
  16.   b:=Tstringlist.create;
  17.   a.Addstrings(ca);
  18.   b.AddStrings(cb);
  19.   writeln(JoinSLs(a,b).Text);
  20.   b.free;
  21.   a.free;  
  22. end.

It would help if you read the excellent documentation...

As a teaser you can also do it like this:
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode objfpc}{$H+}{$endif}
  2. uses classes;
  3. const
  4.   ca:array[0..2] of string = ('a','b','c');
  5.   cb:array[0..2] of string = ('d','e','f');
  6.  
  7.   operator + (const a,b:Tstrings):Tstrings;
  8.   begin
  9.     Result := a;
  10.     Result.AddStrings(b);
  11.   end;
  12.    
  13. var a,b:Tstrings;
  14. begin
  15.   a:=Tstringlist.create;
  16.   b:=Tstringlist.create;
  17.   a.Addstrings(ca);
  18.   b.AddStrings(cb);
  19.   writeln((a + b).Text);
  20.   b.free;
  21.   a.free;  
  22. end.
« Last Edit: July 19, 2018, 10:40:10 pm by Thaddy »
Specialize a type, not a var.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Adding stringlists optimized
« Reply #6 on: July 20, 2018, 04:20:22 am »
Code: Pascal  [Select][+][-]
  1. New := JoinSLs(Old,New); // wrong values

Please refrain from this type of usage, because:
  • You are not freeing New before assignment. That is a memory leak.
  • You are assigning Old to New through the code of this function. Later when you free one of them you are also, unintentionally, freeing the other.

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Adding stringlists optimized
« Reply #7 on: July 20, 2018, 08:33:01 pm »
Code: Pascal  [Select][+][-]
  1. New := JoinSLs(Old,New); // wrong values

Please refrain from this type of usage, because:
  • You are not freeing New before assignment. That is a memory leak.
  • You are assigning Old to New through the code of this function. Later when you free one of them you are also, unintentionally, freeing the other.

Yes, my aproaching was incorrect, and even worst, New and Old are both global variables.  I had a misunderstood of how stringlists managed the data, so this was the reason i posted here to ask.
I finally take the shorter way, which if not the better, is working pretty well.
Without calling external functions:

Code: Pascal  [Select][+][-]
  1. OldStringList.AddStrings(NewStringlist);
  2. NewStringList.Clear;
  3. NewStringList.AddStrings(OldStringList)

AFAIK it can not be optimizied beyond this point... or im wrong again?
(optimization for this is pretty important since this is a VERY common situation in my app)
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Adding stringlists optimized
« Reply #8 on: July 20, 2018, 10:03:27 pm »
Please refrain from this type of usage, because:
Indeed. Hence const was used in both my examples. For briefness I did not test for nil. const demands an initialized stringlist in this case and the compiler will show it at compile time.
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Adding stringlists optimized
« Reply #9 on: July 20, 2018, 10:10:16 pm »
Code: Pascal  [Select][+][-]
  1. OldStringList.AddStrings(NewStringlist);
  2. NewStringList.Clear;
  3. NewStringList.AddStrings(OldStringList)
AFAIK it can not be optimizied beyond this point... or im wrong again?
(optimization for this is pretty important since this is a VERY common situation in my app)

Well:
Code: Pascal  [Select][+][-]
  1. OldStringList.AddStrings(NewStringlist);
  2. NewStringList.Assign(OldStringList);
Is enough in your particular case. If you re-use it, you of course need to clear it.
« Last Edit: July 20, 2018, 10:15:08 pm by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018