Recent

Author Topic: Edit text file  (Read 446 times)

AY

  • Newbie
  • Posts: 3
Edit text file
« on: May 29, 2019, 09:46:20 am »
hello,

i have a text file with 20 numbers. i want to add '5' to every alternate index of the file. Is it possible?
« Last Edit: May 29, 2019, 10:54:07 am by AY »

RAW

  • Hero Member
  • *****
  • Posts: 755
Re: Edit text file
« Reply #1 on: May 29, 2019, 11:18:44 am »
Sorry, you need to use JAVA for this!

Edit: Yes, I have no idea what "every alternate index" might be ...  :D
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.  sl: TStringlist;
  4.  i : Integer;
  5. begin
  6.   sl:= TStringlist.Create;
  7.    Try
  8.     sl.LoadFromFile(Application.Location+'MyFile.txt');
  9.      For i:= sl.Count-1 Downto 0
  10.      Do sl[i]:= sl[i]+'5';
  11.     sl.SaveToFile(Application.Location+'MyFile.txt');
  12.    Finally
  13.     sl.Free;
  14.    End;
  15. end;
« Last Edit: May 29, 2019, 11:46:25 am by RAW »
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.2 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1

jamie

  • Hero Member
  • *****
  • Posts: 1656
Re: Edit text file
« Reply #2 on: May 29, 2019, 01:23:28 pm »
He stated alternate index, that to me means every other index.

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.  
  3. var
  4. sl: TStringlist;
  5. i : Integer;
  6. begin
  7. sl:= TStringlist.Create;
  8. Try
  9.  sl.LoadFromFile(Application.Location+'MyFile.txt');
  10. For i:= sl.Count-1 Downto 0
  11. Do  If  I and 1 <> 0 Then Sl[I] := IntTostr(StrToInt(Sl[I])+5);  
  12. sl.SaveToFile(Application.Location+'MyFile.txt');
  13.  Finally
  14.  sl.Free;
  15.  End;
  16. end;
  17.  
« Last Edit: May 29, 2019, 01:25:00 pm by jamie »

AY

  • Newbie
  • Posts: 3
Re: Edit text file
« Reply #3 on: May 29, 2019, 02:38:36 pm »
Thanku jamie and raw, this much works :)

Jamie i used your code

I want to add little more complex thing to the code

What should i edit if i want that the addition takes place, only when a certain value is increased.

For example: 5 is only added when value of x(kind of index) increases to 2 from 1 till s1.count, and it should add to values only  to index which are after value of x

jamie

  • Hero Member
  • *****
  • Posts: 1656
Re: Edit text file
« Reply #4 on: May 29, 2019, 07:07:31 pm »
You will need to use some check points before you follow through with code.
Example:

 if StrToInt(si) < X Then { continue on }

AY

  • Newbie
  • Posts: 3
Re: Edit text file
« Reply #5 on: June 18, 2019, 09:52:51 am »
hey jamie,

the (si), is si or s1? and where should i insert this line in the code?

Thanku

jamie

  • Hero Member
  • *****
  • Posts: 1656
Re: Edit text file
« Reply #6 on: June 18, 2019, 10:54:37 pm »
Well I guess it's "S1"

Code: Pascal  [Select]
  1.  
  2. If StrToint( S1[I] ) > SomeValue thenn….
  3.  
  4.