Recent

Author Topic: TMemo Question  (Read 4724 times)

greertr

  • Full Member
  • ***
  • Posts: 113
    • Virtual Pilot Dashboard
TMemo Question
« on: February 09, 2017, 04:46:41 pm »
I have a tmemo which I will populate with the folloing kind of data...
Code: Pascal  [Select][+][-]
  1.  3   9.037802778    7.285102778     1158    11630    40     -2.000  ABC ENRT DN ABUJA VOR/DME
  2. 12   9.037802778    7.285102778     1158    11630    40      0.000  ABC ENRT DN ABUJA VOR/DME
  3.  3  11.549211111   43.154819444       49    11460    40      2.000  ABI ENRT HD DJIBOUTI TACAN
  4. 12  11.549211111   43.154819444       49    11460    40      0.000  ABI ENRT HD DJIBOUTI TACAN DME
  5.  3  32.462833333   13.169508333      489    11510   130      2.000  ABU ENRT HL ABU ARGUB VOR/DME
  6. 12  32.462833333   13.169508333      489    11510   130      0.000  ABU ENRT HL ABU ARGUB VOR/DME
  7.  3 -28.570652778   16.533833333       98    11210   130    -19.000  ABV ENRT FA ALEXANDER BAY VOR/DME
  8. 12 -28.570652778   16.533833333       98    11210   130      0.000  ABV ENRT FA ALEXANDER BAY VOR/DME

I've been playing arund with a for to do loop to iterate through the tmemo data, line by line, but keep getting errors.

I've been trying to use strutils delspace1 and trim.

Here is the code from my last attempt...
Code: Pascal  [Select][+][-]
  1. procedure TForm5.Button12Click(Sender: TObject);  //del all but one spaces
  2. var
  3.  S: String;
  4.  lineno: integer;
  5. begin
  6.   lineno := 1;
  7.   For memo1.lines[lineno] := 1 to memo1.lines[lastindex] do
  8.     begin
  9.       S := memo1.lines[lineno];
  10.       DelSpace1 (S);
  11.       memo1.lines[lineno] := S;
  12.       //lineno = lineno + 1;
  13.     end;
  14. end;

Questions:
1.  How do I iterate the lines?  I get an erro using memo1.lines[lastindex]
2.  not sure how to use the strutils routines in the graphical environment.

thx in adv

Thaddy

  • Hero Member
  • *****
  • Posts: 18765
  • To Europe: simply sell USA bonds: dollar collapses
Re: TMemo Question
« Reply #1 on: February 09, 2017, 04:55:28 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.  S: String;
  4.  lineno: integer;
  5. begin
  6.   For Lineno := 0 to memo1.lines.count-1 do
  7.       DelSpace1 (memo1.lines[lineno]);
  8. end;

If you use trunk fpc (maybe this is already in fpc 3) it becomes:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.  S: String;
  4. begin
  5.   For s in memo1.lines do
  6.       DelSpace1 (S);
  7. end;

The strutils unit will simply mostly work, depending on string type. Lazarus uses UTF8 by default

« Last Edit: February 09, 2017, 05:08:39 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

greertr

  • Full Member
  • ***
  • Posts: 113
    • Virtual Pilot Dashboard
Re: TMemo Question
« Reply #2 on: February 09, 2017, 05:10:59 pm »
thx Thaddy -- but I'm getting a compiler error about fatal error: illegal character "'A'" (C$2)

get that same error for both code examples you provided

...but the code you provided intuitively looks right to me.
« Last Edit: February 09, 2017, 05:18:46 pm by greertr »

Thaddy

  • Hero Member
  • *****
  • Posts: 18765
  • To Europe: simply sell USA bonds: dollar collapses
Re: TMemo Question
« Reply #3 on: February 09, 2017, 05:21:59 pm »
Can you show us the DelSpace1 routine? (I simulated that while testing)
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

greertr

  • Full Member
  • ***
  • Posts: 113
    • Virtual Pilot Dashboard
Re: TMemo Question
« Reply #4 on: February 09, 2017, 05:23:13 pm »
do you mean this...
Code: Pascal  [Select][+][-]
  1. procedure TForm5.Button11Click(Sender: TObject);
  2. begin
  3.  
  4. end;
  5.  
  6. procedure TForm5.Button12Click(Sender: TObject);
  7.   var
  8.     S: String ='';
  9. begin
  10.   For S in memo1.lines do
  11.       DelSpace1 (S);
  12. end;
  13.  
  14. {  var
  15.    S: String;
  16.    lineno: integer;
  17.   begin
  18.     For Lineno := 0 to memo1.lines.count-1 do
  19.       begin
  20.         S := memo1.lines[lineno];
  21.         DelSpace1 (S);
  22.         memo1.lines[lineno] := S;
  23.       end;
  24.   end;   }

Thaddy

  • Hero Member
  • *****
  • Posts: 18765
  • To Europe: simply sell USA bonds: dollar collapses
Re: TMemo Question
« Reply #5 on: February 09, 2017, 05:32:44 pm »
No DelSpace1 (S) is a non-standard procedure or function that you probably wrote yourself..
Code: Pascal  [Select][+][-]
  1. procedure DelSpace1 (var S:string);
  2. begin
  3.   // what is here?
  4. end;
  5.  
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

greertr

  • Full Member
  • ***
  • Posts: 113
    • Virtual Pilot Dashboard
Re: TMemo Question
« Reply #6 on: February 09, 2017, 05:38:09 pm »
it is a function in strutils that is supposed to remove all but one space in a sequence of spaces in  a string.

Code: Pascal  [Select][+][-]
  1. function DelSpace1(const S: string): string;
  2.  
  3. var
  4.   i: Integer;
  5.  
  6. begin
  7.   Result:=S;
  8.   for i:=Length(Result) downto 2 do
  9.     if (Result[i]=' ') and (Result[I-1]=' ') then
  10.       Delete(Result,I,1);
  11. end;

p.s. got the error resolved...it was invisible illegal characcters crept in some lines when i was copying.  so now it executes but doesn't delete the extra spaces
« Last Edit: February 09, 2017, 06:50:09 pm by greertr »

ASerge

  • Hero Member
  • *****
  • Posts: 2477
Re: TMemo Question
« Reply #7 on: February 09, 2017, 06:50:47 pm »
The above code (except DelSpace1) is not correct  >:D
Use simple Memo1.Text := DelSpace1(Memo1.Text) ;)
« Last Edit: February 09, 2017, 06:53:19 pm by ASerge »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TMemo Question
« Reply #8 on: February 09, 2017, 07:34:22 pm »
To be clear - ASerge's simple soution would be
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   Memo1.Text:=DelSpace1(Memo1.Text);
  4. end;

Thaddy's 2nd solution
Code: Pascal  [Select][+][-]
  1. For s in memo1.lines do
  2.   DelSpace1(s);
will not work, since local placeholder variables (such as s above) used in enumerators are read-only in FPC.

greertr

  • Full Member
  • ***
  • Posts: 113
    • Virtual Pilot Dashboard
Re: TMemo Question
« Reply #9 on: February 09, 2017, 07:38:10 pm »
thx guys!

got it working now

 

TinyPortal © 2005-2018