Recent

Author Topic: Change of Delimited Text Order in Text File  (Read 1954 times)

Boleeman

  • Sr. Member
  • ****
  • Posts: 433
Re: Change of Delimited Text Order in Text File
« Reply #15 on: January 24, 2023, 08:28:51 pm »
Thanks ASerge. Wow, so your code essentially gets rid of one listbox. I went on an experimented.
Just created and used another stringlist and got rid of the Listbox2.

I somehow don't fully understand        if A = nil then   as it sounds like if the split Array has nothing in it then continue which seems wrong


Possibly if the items making up Array A from the test text file have been all split then continue which seems more likely.

Thanks ASerge for the updated code. It works brilliantly. Love those Last, Insert, Delete parts.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.bn_doItClick(Sender: TObject);
  2. var
  3.   L, M: TStringList;
  4.   S, Last: string;
  5.   A: TStringArray;
  6. begin
  7.   ListBox2.Clear;
  8.   L := TStringList.Create;
  9.   M := TStringList.Create;
  10.   try
  11.     L.LoadFromFile('test.txt');
  12.     for S in L do
  13.     begin
  14.       A := S.Split(';');
  15.       if A = nil then
  16.         Continue;
  17.       Last := A[High(A)];
  18.       Insert(Last, A, 0);
  19.       Delete(A, High(A), 1);
  20.       M.Append(''.Join(';', A));
  21.       M.SaveToFile('new_test.txt');
  22.     end;
  23.   finally
  24.     L.Free;
  25.     M.Free;
  26.   end;
  27.  
  28. end;          
« Last Edit: January 24, 2023, 08:46:41 pm by Boleeman »

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: Change of Delimited Text Order in Text File
« Reply #16 on: January 25, 2023, 02:08:56 pm »
Because docs say something different --> https://www.freepascal.org/docs-html/rtl/system/insert.html
Either insert a String "Source" into String "S" at Position, or insert DynArray Source into DynArray S at Index

Your code looks like "Insert String Source into DynArray S at Index".

Or is this undocumented behavior?
Since this behavior is not documented, it turns out that this is an undocumented behavior :)
System.Insert allows not only an array, but also a regular element as the first parameter:
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$MODE OBJFPC}
  3.  
  4. procedure Test;
  5. var
  6.   A: TBoundArray = (1, 2);
  7.   B: TBoundArray = (3, 4, 5);
  8.   C: SizeInt = 0;
  9. begin
  10.   Insert(A, B, 0);
  11.   Insert(C, B, 0);
  12.   for C in B do
  13.     Write(C, ' ');
  14.   Writeln;
  15. end;
  16.  
  17. begin
  18.   Test;
  19.   Readln;
  20. end.

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: Change of Delimited Text Order in Text File
« Reply #17 on: January 25, 2023, 02:12:29 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.bn_doItClick(Sender: TObject);
  2. ...
  3.     for S in L do
  4.     begin
  5. ...
  6.       M.Append(''.Join(';', A));
  7.       M.SaveToFile('new_test.txt');
  8.     end;
  9. ...
It's probably better to make a SaveToFile call outside of the loop.

TRon

  • Hero Member
  • *****
  • Posts: 2515

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Change of Delimited Text Order in Text File
« Reply #19 on: January 25, 2023, 02:39:37 pm »
Since this behavior is not documented, it turns out that this is an undocumented behavior :)
Would/Could you open a Documentation-Bugreport for this?

because there should be a third Function-Signature in this case
Code: Pascal  [Select][+][-]
  1. procedure Insert(
  2.   const source: string;
  3.   var S: string;
  4.   const Index: Integer
  5. );
  6.  
  7. procedure Insert(
  8.   const source: DynaArrayType;
  9.   var S: DynArrayType;
  10.   const Index: Integer
  11. );
  12. //Missing
  13. procedure Insert(
  14.   const source: String;
  15.   var S: DynArrayType;
  16.   const Index: Integer
  17. );
« Last Edit: January 25, 2023, 02:41:42 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Change of Delimited Text Order in Text File
« Reply #20 on: January 25, 2023, 02:45:17 pm »
No, because the behavior is documented as per the documentation reference above.
What is warranted though, is to file a bug report and ask for further explicit clarification.
Good luck...
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Change of Delimited Text Order in Text File
« Reply #21 on: January 25, 2023, 02:48:33 pm »
No, because the behavior is documented as per the documentation reference above.
What is warranted though, is to file a bug report and ask for further explicit clarification.
Good luck...
Which reference do you mean?
My Link to the Documentation, or the Link to the Wiki?
Because as many times as it has been said, the Wiki is not the documentation.....

Though you are correct, that it is mentioned in the Wiki.
I was refering to the "official" doc's, where the two signatures are displayed, which imply that inserting a String to a DynArray SHOULD NOT work
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

bytebites

  • Hero Member
  • *****
  • Posts: 640
Re: Change of Delimited Text Order in Text File
« Reply #22 on: January 25, 2023, 03:37:35 pm »
Inserting string to dynamic array of string works, nothing peculiar in it.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Change of Delimited Text Order in Text File
« Reply #23 on: January 25, 2023, 07:45:33 pm »
In principle I would not refer to the wiki as official documentation, unless I verified it was correct, then I might provide a link.
So I mean the link that refers to "the documentation", not a wiki entry. There are more links in the docs that may help, I will add them later.
https://www.freepascal.org/docs-html/rtl/system/insert.html
https://www.freepascal.org/docs-html/rtl/classes/tstrings.insert.html  (abstract)
https://www.freepascal.org/docs-html/rtl/classes/tstringlist.insert.html (implementation )

The documentation is consistent over all three entries....
« Last Edit: January 25, 2023, 07:51:38 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018