Recent

Author Topic: Parsing error?  (Read 495 times)

jlinux

  • New Member
  • *
  • Posts: 17
Parsing error?
« on: July 14, 2026, 08:03:30 pm »
When extracting items from a string list one by one in a for loop, the compiler reports an error.
Code: Pascal  [Select][+][-]
  1. mm: TMemo;
  2. I: Integer;
  3. S: String;
  4. SL: TStringList;
  5. ...
  6. for i := 0 to SL.Count-1 do begin
  7.   mm.Append(SL.Strings[i]); // <<< Error
  8.   mm.Lines.Add(SL.Strings[i]); // <<< Error
  9.  
But:
Code: Pascal  [Select][+][-]
  1. for i := 0 to SL.Count-1 do begin
  2.   s := SL.Strings[i];
  3.   mm.Append(s); // <<< OK
  4.   mm.Lines.Add(s); // <<< OK
  5. end;
  6.  
Syntactically, both forms seem correct to me, so is this a known bug or some other strange situation that I’m missing?
« Last Edit: July 14, 2026, 08:06:45 pm by jlinux »

cdbc

  • Hero Member
  • *****
  • Posts: 2931
    • http://www.cdbc.dk
Re: Parsing error?
« Reply #1 on: July 14, 2026, 08:26:52 pm »
Hi
Try this:
Code: Pascal  [Select][+][-]
  1. mm: TMemo;
  2. I: Integer;
  3. SL: TStringList;
  4. ...
  5. for i := 0 to SL.Count-1 do begin
  6.   mm.Append(SL[i]); // Strings is declared 'default',
  7.   mm.Lines.Add(SL[i]); // thus you can skip the property name
  8. end;
  9.  
Alternatively, if you want the whole stringlist stuffed into the memo, you can:
Code: Pascal  [Select][+][-]
  1. mm.Clear;
  2. mm.Lines.Assign(SL);
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

dsiders

  • Hero Member
  • *****
  • Posts: 1679
Re: Parsing error?
« Reply #2 on: July 14, 2026, 08:55:49 pm »
When extracting items from a string list one by one in a for loop, the compiler reports an error.
Code: Pascal  [Select][+][-]
  1. mm: TMemo;
  2. I: Integer;
  3. S: String;
  4. SL: TStringList;
  5. ...
  6. for i := 0 to SL.Count-1 do begin
  7.   mm.Append(SL.Strings[i]); // <<< Error
  8.   mm.Lines.Add(SL.Strings[i]); // <<< Error
  9.  
But:
Code: Pascal  [Select][+][-]
  1. for i := 0 to SL.Count-1 do begin
  2.   s := SL.Strings[i];
  3.   mm.Append(s); // <<< OK
  4.   mm.Lines.Add(s); // <<< OK
  5. end;
  6.  
Syntactically, both forms seem correct to me, so is this a known bug or some other strange situation that I’m missing?

I tried the current main branch using:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Memo1: TMemo;
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure FormCreate(Sender: TObject);
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. uses LazFileUtils;
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36. procedure TForm1.Button1Click(Sender: TObject);
  37. var
  38.   SL: TStringList;
  39.   iCnt, iLine: Integer;
  40. begin
  41.   Memo1.Clear;
  42.  
  43.   SL := TStringList.Create;
  44.   SL.LoadFromFile(CleanAndExpandFileName('~/lazarus/README.md'));
  45.   iCnt := SL.Count;
  46.  
  47.   for iLine := 0 to iCnt-1 do
  48.   begin
  49.     // skip blank lines
  50.     if (Trim(SL[iLine]) = '') then continue;
  51.  
  52.     Memo1.Lines.Add(SL.Strings[iLine]);
  53.     Memo1.Append(SL.Strings[iLine]);
  54.     Memo1.Lines.Add(SL[iLine]);
  55.     Memo1.Append(SL[iLine]);
  56.   end;
  57.   SL.Free;
  58.   Memo1.SelStart := 0;
  59. end;
  60.  
  61. procedure TForm1.FormCreate(Sender: TObject);
  62. begin
  63.   Height := Screen.WorkAreaHeight - 96;
  64.   Position := poScreenCenter;
  65. end;
  66.  
  67. end.
  68.  

All of the variants worked as expected - producing the attached screenshot.

It might help if we knew what version of Lazarus/ FPC you're using.






jlinux

  • New Member
  • *
  • Posts: 17
Re: Parsing error?
« Reply #3 on: July 14, 2026, 10:30:04 pm »
Hi Benny,  thanks for replying.
Yes, `assign` works.
The issue here is when I try to extract just a few items.
By the way, the FPC version is 3.8 on Windows 11
//----------------
Hi desiders, thanks for your reply and the example.
I can’t see any difference, but I’ll take your example and going to test it.

Best Regards

bytebites

  • Hero Member
  • *****
  • Posts: 794
Re: Parsing error?
« Reply #4 on: July 14, 2026, 11:05:16 pm »
Op doesn't bother to tell precisely which error he gets.

Bart

  • Hero Member
  • *****
  • Posts: 5757
    • Bart en Mariska's Webstek
Re: Parsing error?
« Reply #5 on: July 14, 2026, 11:36:56 pm »
When extracting items from a string list one by one in a for loop, the compiler reports an error.
Code: Pascal  [Select][+][-]
  1. mm: TMemo;
  2. I: Integer;
  3. S: String;
  4. SL: TStringList;
  5. ...
  6. for i := 0 to SL.Count-1 do begin
  7.   mm.Append(SL.Strings[i]); // <<< Error
  8.   mm.Lines.Add(SL.Strings[i]); // <<< Error
  9.  

Compiles just fine (added the missing "end;") for me with fpc 3.2.4RC1.

What compiler error are you getting exactly?
(Right-click the message in teh Message View windo and select "COpy selected message")

Bart

jlinux

  • New Member
  • *
  • Posts: 17
Re: Parsing error?
« Reply #6 on: July 15, 2026, 02:45:35 am »
bytebits,
It wasn’t that I didn’t want to bother mentioning the error; I simply forgot. My bad.

Bart,
If I remember correctly, it was something like [Error: incorrect arg, got: ‘AnsiString’, expected: ‘QWORD’], which made no sense at all, and that’s why I decided to take a different approach and come here to ask.
One thing I know for sure is that I tried several times, checked the code over and over, but I always got this error—and strangely, it worked with “assign” or with the alternative I posted.
I’m trying to remember this because, in the meantime, after restart the pc and a few dozen more lines of code and some fixes, now i test it again and the error stopped appearing,so I no longer had access to the exact error message. Don’t ask me why.
High-level stuff isn’t really my cup of tea, but hardware/firmware.

Ty to everyone who replied.
Best regards,
Jorge

cdbc

  • Hero Member
  • *****
  • Posts: 2931
    • http://www.cdbc.dk
Re: Parsing error?
« Reply #7 on: July 15, 2026, 09:33:22 am »
Hi again Jorge
If you feel like it, you should /swing/ by THIS little repo, which sports an assorted array of containers, implemented as COM-Objects / interfaces.
Especially cdbc.istrlist.pas, has a few extra tricks up its sleeve:
· AddOrSet()
· AssignToEx
· Iterators that don't /forget/ the objects in the list
· ScanFor()
· Property AsBytes: TBytes { great with db-blobs }
etc. etc.
Should you have questions, then just ask them here in the forum...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Thaddy

  • Hero Member
  • *****
  • Posts: 19625
  • Glad to be alive.
Re: Parsing error?
« Reply #8 on: July 15, 2026, 12:02:02 pm »
You should simply use assign() as explained above. Iterating over the strings yourself is bad coding.
Trying to use the strings property conflicts, because it has getters and setters. it does not access the list directly.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses classes;
  3. var
  4.   sl,mm:Tstrings;// memo.lines is also TStrings.
  5.   s:string;
  6.   i:integer;
  7. begin
  8.   sl := TSTringlist.Create;
  9.   mm := TStringlist.Create;
  10.   try
  11.     //List1.Assign(List2); // best option for memo use memo.lines.assign
  12.     //------------------------------------
  13.     //for s in sl do // second best option
  14.     //  mm.add(s);
  15.     //------------------------------------
  16.     // worst option:
  17.     for i := 0 to sl.count-1 do
  18.       mm.add(sl.strings[i]) // for a memo this works too, but you can use memo.lines.add()
  19.   finally
  20.     mm.free;
  21.     sl.Free;
  22.   end;
  23. end.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   sl:TStringlist;
  4. begin
  5.   sl := Tstringlist.Create;
  6.   sl.add('Test');  // just as many as you select
  7.   memo1.lines.Assign(sl); // or preserving, use memo1.lines.addstrings(sl)
  8.   sl.Free;
  9. end;
Which is basically the same as demonstrated above.
The strings[] property is useless anyway. Nobody uses it.(except you, it seems)
« Last Edit: July 16, 2026, 07:47:59 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

jlinux

  • New Member
  • *
  • Posts: 17
Re: Parsing error?
« Reply #9 on: July 19, 2026, 02:15:42 am »
First of all, my apologies for the late reply. Work doesn’t wait.

Benny, thank you, I’ll have take a look at that.

Thaddy, my question isn’t meant to argue against your point, but simply to ask you to explain why you say it’s bad coding.
Although I’m of the old school, I’m always open to learn.

Best regards,
Jorge
« Last Edit: July 19, 2026, 02:17:45 am by jlinux »

 

TinyPortal © 2005-2018