Recent

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

Boleeman

  • Sr. Member
  • ****
  • Posts: 433
Change of Delimited Text Order in Text File
« on: January 22, 2023, 11:07:03 am »
Hi Folks.

Made a small program using 2 listboxes and a command button to change the order of 10 delimited string values (separated by semicolons). Wanted the last word of each line in the text file to be first.

If I change the value of i in    T := RowArray[i].Split(';');      for a specific line it works for that line

Almost like the looping is not working.

I applied the steps:
1.  Load text file in listbox1
2. Make a RowArray for each listbox row.
3  Split each listbox1 row
4. Make a new order of the strings.
5. Loop through the listbox1 by count and add to listbox2.

Here is my code:

Code: Pascal  [Select][+][-]
  1. unit delimited;
  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.     bn_doIt: TButton;
  16.     ListBox1: TListBox;
  17.     ListBox2: TListBox;
  18.     procedure bn_doItClick(Sender: TObject);
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.bn_doItClick(Sender: TObject);
  35. var
  36.   T, RowArray: array of string;
  37.   i : Integer;
  38.  
  39. begin
  40.   ListBox1.Items.LoadFromFile('Test.txt');
  41.  
  42.   i := 0;
  43.   T:= Nil;
  44.   RowArray := Nil;
  45.   RowArray:= ListBox1.Items.ToStringArray;
  46.         begin
  47.             for i := 0 to ListBox1.Items.Count - 1 do
  48.                 T := RowArray[i].Split(';');
  49.                 ListBox2.Items.Add(T[10] + ';' + T[0] + ';' + T[1] + ';' + T[2] + ';' + T[3]
  50.              + ';' + T[4] + ';' + T[5] + ';' + T[6] + ';' + T[7] + ';' + T[8] + ';' + T[9]);
  51.         end;
  52. end;
  53.  
  54.  
  55. end.

« Last Edit: January 22, 2023, 11:40:00 am by Boleeman »

alpine

  • Hero Member
  • *****
  • Posts: 1062
Re: Change of Delimited Text Order in Text File
« Reply #1 on: January 22, 2023, 11:18:13 am »
Don't you think the
Code: Pascal  [Select][+][-]
  1. RowArray := Nil;
  2. RowArray:= ListBox1.Items.ToStringArray;
should be before/outside the for loop?
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Boleeman

  • Sr. Member
  • ****
  • Posts: 433
Re: Change of Delimited Text Order in Text File
« Reply #2 on: January 22, 2023, 11:24:32 am »
Yes I made some changes before and forgot to change that. Still not working (only get the last line).


This part does not increment for i = 0, 1, 2


for i := 0 to ListBox1.Items.Count - 1 do
 
 
      T := RowArray.Split(';');
      ListBox2.Items.Add(T[10] + ';' + T[0] + ';' + T[1] + ';' + T[2] + ';' + T[3]
   + ';' + T[4] + ';' + T[5] + ';' + T[6] + ';' + T[7] + ';' + T[8] + ';' + T[9]);
« Last Edit: January 22, 2023, 11:33:43 am by Boleeman »

alpine

  • Hero Member
  • *****
  • Posts: 1062
Re: Change of Delimited Text Order in Text File
« Reply #3 on: January 22, 2023, 11:32:55 am »
Yes I made some changes before and forgot to change that. Still not working (only get the last line).

You don't have begin ... end after the for. Only indentation.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Boleeman

  • Sr. Member
  • ****
  • Posts: 433
Re: Change of Delimited Text Order in Text File
« Reply #4 on: January 22, 2023, 11:38:29 am »
Tried with

Code: Pascal  [Select][+][-]
  1. procedure TForm1.bn_doItClick(Sender: TObject);
  2. var
  3.   T, RowArray: array of string;
  4.   i : Integer;
  5.  
  6. begin
  7.   ListBox1.Items.LoadFromFile('Test.txt');
  8.  
  9.   i := 0;
  10.   T:= Nil;
  11.   RowArray := Nil;
  12.   RowArray:= ListBox1.Items.ToStringArray;
  13.         begin
  14.             for i := 0 to ListBox1.Items.Count - 1 do
  15.                 T := RowArray[i].Split(';');
  16.                 ListBox2.Items.Add(T[10] + ';' + T[0] + ';' + T[1] + ';' + T[2] + ';' + T[3]
  17.              + ';' + T[4] + ';' + T[5] + ';' + T[6] + ';' + T[7] + ';' + T[8] + ';' + T[9]);
  18.         end;
  19. end;
  20.  
  21.  
  22. end.

alpine

  • Hero Member
  • *****
  • Posts: 1062
Re: Change of Delimited Text Order in Text File
« Reply #5 on: January 22, 2023, 11:40:44 am »
Try
Code: Pascal  [Select][+][-]
  1. procedure TForm1.bn_doItClick(Sender: TObject);
  2. var
  3.   T, RowArray: array of string;
  4.   i: integer;
  5.  
  6. begin
  7.   ListBox1.Items.LoadFromFile('Test.txt');
  8.   begin
  9.  
  10.     i := 0;
  11.     T := nil;
  12.  
  13.     RowArray := nil;
  14.     RowArray := ListBox1.Items.ToStringArray;
  15.  
  16.     for i := 0 to ListBox1.Items.Count - 1 do
  17.     begin
  18.       T := RowArray[i].Split(';');
  19.       ListBox2.Items.Add(T[10] + ';' + T[0] + ';' + T[1] + ';' +
  20.         T[2] + ';' + T[3] + ';' + T[4] + ';' + T[5] + ';' + T[6] + ';' +
  21.         T[7] + ';' + T[8] + ';' + T[9]);
  22.     end;
  23.   end;
  24. end;
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Boleeman

  • Sr. Member
  • ****
  • Posts: 433
Re: Change of Delimited Text Order in Text File
« Reply #6 on: January 22, 2023, 11:47:31 am »
Yes that works. Wow. Thanks.

Still learning. Tried all sorts of combinations.


 My Begin and End were all over the place. Now I see where I went wrong.


Thanks Alpine for your help.
« Last Edit: January 22, 2023, 11:50:38 am by Boleeman »

alpine

  • Hero Member
  • *****
  • Posts: 1062
Re: Change of Delimited Text Order in Text File
« Reply #7 on: January 22, 2023, 11:57:57 am »
No problem.
You can do it even better by using dynarray intrinsics (Length, Low, High, etc.).
Code: Pascal  [Select][+][-]
  1. procedure TForm1.bn_doItClick(Sender: TObject);
  2. var
  3.   T, RowArray: array of string;
  4.   i, j: integer;
  5.   S: String;
  6. begin
  7.   ListBox1.Items.LoadFromFile('Test.txt');
  8.   begin
  9.  
  10.     i := 0;
  11.     T := nil;
  12.  
  13.     RowArray := nil;
  14.     RowArray := ListBox1.Items.ToStringArray;
  15.  
  16.     for i := 0 to ListBox1.Items.Count - 1 do
  17.     begin
  18.       T := RowArray[i].Split(';');
  19.       S := '';
  20.       if Length(T) > 0 then
  21.       begin
  22.         S := T[High(T)];
  23.         for j := 0 to High(T) - 1 do
  24.           S := S + ';' + T[j];
  25.       end;
  26.       ListBox2.Items.Add(S);
  27.     end;
  28.   end;
  29. end;  

Thus not required each line to have exactly 10 fields.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Boleeman

  • Sr. Member
  • ****
  • Posts: 433
Re: Change of Delimited Text Order in Text File
« Reply #8 on: January 22, 2023, 01:04:38 pm »
Wow. Even better.

I actually wanted to use string arrays without listboxes but needed something simple to start off with.

Tried out your new code. Works well. Like seeing how things can be done in different ways.

Thanks for the updated code. Much appreciated.


So    S := T[High(T)];      gives the   highest Array element of array T

Did not know this.

Thought that typing out:
T[10] + ';' + T[0] + ';' + T[1] + ';' +
            T[2] + ';' + T[3] + ';' + T[4] + ';' + T[5] + ';' + T[6] + ';' +
            T[7] + ';' + T[8] + ';' + T[9]);

was a bit too much and mistake prone.


I will test the other way as well like    S := T[Low(T)];    would give lowest Array element of array T

Would you use this if you wanted to go the other way like with letter at the back again.

Crossing;1.25;1.5;8;0.1;1.45;0.45;350;8;0;16711935

to

1.25;1.5;8;0.1;1.45;0.45;350;8;0;16711935;Crossing



« Last Edit: January 22, 2023, 01:22:12 pm by Boleeman »

alpine

  • Hero Member
  • *****
  • Posts: 1062
Re: Change of Delimited Text Order in Text File
« Reply #9 on: January 22, 2023, 01:42:33 pm »
I will test the other way as well like    S := T[Low(T)];    would give lowest Array element of array T

Low(T) for a dynamic array is 0, it makes sense for declared arrays where the lower bound can be something different.
 
Would you use this if you wanted to go the other way like with letter at the back again.

Crossing;1.25;1.5;8;0.1;1.45;0.45;350;8;0;16711935

to

1.25;1.5;8;0.1;1.45;0.45;350;8;0;16711935;Crossing
Then you should probably change this
Code: Pascal  [Select][+][-]
  1.       ...
  2.       if Length(T) > 0 then
  3.       begin
  4.         S := T[High(T)];
  5.         for j := 0 to High(T) - 1 do
  6.           S := S + ';' + T[j];
  7.       end;
  8.       ...

to:
Code: Pascal  [Select][+][-]
  1.       ...
  2.       if Length(T) > 0 then
  3.       begin
  4.         for j := 1 to High(T) do // <-- from 1 to the highest
  5.           S := S + T[j] + ';'; // <-- put the semicolon after
  6.         S := T[0];
  7.       end;
  8.       ...
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Boleeman

  • Sr. Member
  • ****
  • Posts: 433
Re: Change of Delimited Text Order in Text File
« Reply #10 on: January 22, 2023, 03:05:53 pm »
Small correction:

      if Length(T) > 0 then
       begin
         for j := 1 to High(T) do // <-- from 1 to the highest
           S := S + T[j] + ';'; // <-- put the semicolon after
          S := S + T[0];
       end;             


Wow. Thanks once again Alpine.
« Last Edit: January 22, 2023, 03:08:22 pm by Boleeman »

alpine

  • Hero Member
  • *****
  • Posts: 1062
Re: Change of Delimited Text Order in Text File
« Reply #11 on: January 22, 2023, 03:12:56 pm »
Sorry for the mistake, but obviously you've got the idea
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Boleeman

  • Sr. Member
  • ****
  • Posts: 433
Re: Change of Delimited Text Order in Text File
« Reply #12 on: January 23, 2023, 06:55:45 am »
Apline. Thanks for all your help. It was appreciated.

A helping hand helped me to understand what I was doing wrong.
Felt like I was going round in circles at the time.

ASerge

  • Hero Member
  • *****
  • Posts: 2241
Re: Change of Delimited Text Order in Text File
« Reply #13 on: January 23, 2023, 03:35:13 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.bn_doItClick(Sender: TObject);
  2. var
  3.   L: TStringList;
  4.   S, Last: string;
  5.   A: TStringArray;
  6. begin
  7.   ListBox2.Clear;
  8.   L := TStringList.Create;
  9.   try
  10.     L.LoadFromFile('test.txt');
  11.     for S in L do
  12.     begin
  13.       A := S.Split(';');
  14.       if A = nil then
  15.         Continue;
  16.       Last := A[High(A)];
  17.       Insert(Last, A, 0);
  18.       Delete(A, High(A), 1);
  19.       ListBox2.Items.Append(''.Join(';', A));
  20.     end;
  21.   finally
  22.     L.Free;
  23.   end;
  24. end;

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Change of Delimited Text Order in Text File
« Reply #14 on: January 23, 2023, 03:57:45 pm »
Serge, you sure about Line 17?
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?
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

 

TinyPortal © 2005-2018