Forum > Beginners

TStringList in Windows10 vs Windows7 work

<< < (2/5) > >>

af0815:

--- Quote from: noszone on December 29, 2021, 11:04:55 am ---The code is big and I don't have it complete. But he refused TStringList and trying to use now a database selects instead of TStringList for comparing and etc. Thanks.

--- End quote ---
To use the select in the DB context is IMHO normaly a better approach.

noszone:
Thank you all for replies.


--- Quote from: mas steindorff on December 29, 2021, 07:41:07 pm ---Do you get the exception at start or when you get a lot to info in your dataset?

--- End quote ---

I think when there is a data. He uses a TStringList like a stack for string comparison. New data alaways filling this stack, but the size(length) of TStringList is 20-30 rows. New data rewrites old data on stack and like so.

Thaddy:
I Also used Tstringlist to mimic a stack in the olden days, but now there are enough stacks available. e.g. in generics.collections, fpc-stl etc.
Example:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode delphi}{$H+}uses generics.collections;type  TStringStack = TStack<string>;// if you prefer {$mode objfpc} then   TStringStack = specialize TStack<string>;  var  s:TStringStack;  p:string;begin  s:=Tstringstack.create;  try    s.push('test');    s.Push('test2');    // etc    writeln(s.pop)   finally     s.free;   end;end.This is also delphi compatible.
See also https://docwiki.embarcadero.com/CodeExamples/Sydney/en/Generics_Collections_TStack_(Delphi)

Thaddy:
BTW here a lightweight version based on the stack from the contnrs unit:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit stringstack;{lightweight stringstack ,(c) 2022, Thaddy de Koning. Use as you like}interface{$mode objfpc}uses   contnrs, sysutils;  type  TStringStackException = class(Exception);   TStringItem = record    s:string;  end;  PStringItem = ^TStringItem;    TStringStack = class(TStack)  public    destructor destroy;override;    procedure Clear;    Function Count: Integer;    Function AtLeast(ACount: Integer): Boolean;    function Push(const AItem: string):string;    Function Pop: string;    Function Peek: string;    Function IsEmpty:Boolean;  end;    implementation    Destructor TStringStack.Destroy;  begin     Clear;    inherited destroy;  end;    Procedure TStringStack.Clear;  begin    while not IsEmpty do pop;  end;    Function TStringStack.Count:integer;  begin    Result := Inherited Count;      end;     Function TStringStack.AtLeast(ACount: Integer): Boolean;  begin     Result := Inherited AtLeast(ACount);  end;  {$PUSH}{$WARN 5093 OFF since new() initializes}  function TStringStack.Push(const AItem:String):string;  var    r:PStringItem;  begin    New(r);        r^.s := AItem;    r := Inherited Push(r);    Result := r^.s;  end;    Function TStringStack.Pop:String;  var    t:PStringItem;  begin    if IsEmpty then       raise TStringStackException.create('Pop: error: stack is empty');    t := InHerited pop;    Result := t^.s;    Dispose(t);  end;     Function TStringStack.Peek:String;  var    t:PstringItem;  begin    if IsEmpty then       raise TStringStackException.create('Peek: error: stack is empty');    t := InHerited peek;    Result := t^.s;  end;   {$POP}  Function TStringStack.IsEmpty:Boolean;  begin    Result := Count = 0;  end; end.Useful if you do not want to use generics.
It is also faster than Tstringlist as a stack.

Thaddy:
with hindsight, edited the previous post and added exception handling and simplified the rest a bit.
I added the exception so you can better use it in production code.
Demo:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program sstest;{$mode objfpc}uses stringstack; var   stack:TStringStack; begin{$if declared(UseHeapTrace)}  GlobalSkipIfNoLeaks := True; // supported as of debugger version 3.2.0{$endIf}  stack := TStringStack.Create;  try    try      { AtLeast}      writeln('AtLeast 20?: ',stack.AtLeast(20),,' Count is ',stack.count);      { Push }      writeln(stack.push('test1'),stack.count:2);      { Peek }      writeln('peek: ',stack.peek);      writeln(stack.push('test2'),stack.count:2);      writeln('peek: ',stack.peek);      writeln(stack.push('test3'),stack.count:2);      writeln('AtLeast 2?: ',stack.AtLeast(2),' Count is ',stack.count);;      { Pop }      writeln(stack.pop,stack.count:2);      writeln('peek: ',stack.peek);      writeln(stack.pop,stack.count:2);      writeln('peek: ',stack.peek);      writeln(stack.pop,stack.count:2);      writeln('peek: ',stack.peek);      { this raises exception, as would pop }      writeln(stack.peek,stack.count:2);    except      on E:TStringStackException do        writeln(E.Message)      else        raise;    end;  finally    stack.free;  end;end.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version