Recent

Author Topic: Find word in textfile  (Read 12453 times)

samoraj

  • New Member
  • *
  • Posts: 22
Find word in textfile
« on: July 01, 2017, 06:00:27 am »
Hello, I want to check in a text file, if there is a word, then to set the whole line of the file variable

var
found : boolean;
sl : TStringList;
i : integer;
begin       
found := false;
sl := TStringList.Create;
try
  sl.LoadFromFile(filename);
  for line in sl do
    if Pos('hello', line)<>0 then
    begin
      found := true;
      break;
    end;
finally
  sl.Free;
end;   
end;



but I get this error:
unit1.pas(412,11) Fatal: Syntax error, "THEN" expected but "(" found

Thaddy

  • Hero Member
  • *****
  • Posts: 16937
  • Ceterum censeo Trump esse delendam
Re: Find word in textfile
« Reply #1 on: July 01, 2017, 06:44:14 am »
There is nothing wrong with the code you showed. (besides  missing var line = string;)
So there must be something else wrong.
Here's your code as a full program. It works.
Code: Pascal  [Select][+][-]
  1. program untitled;
  2. {$mode objfpc}
  3. uses classes;
  4. var
  5. found : boolean;
  6. line:string;
  7. sl : TStringList;
  8. begin      
  9. found := false;
  10. sl := TStringList.Create;
  11. if paramcount > 1 then
  12. try
  13.   sl.loadfromfile(Paramstr(1));
  14.   for line in sl do
  15.     if Pos('hello', line)<>0 then
  16.     begin
  17.       found := true;
  18.       break;
  19.     end;
  20. finally
  21.   sl.Free;
  22. end;  
  23. end.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

samoraj

  • New Member
  • *
  • Posts: 22
Re: Find word in textfile
« Reply #2 on: July 01, 2017, 07:19:49 am »
Thanks Thaddy i find my error :)

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Find word in textfile
« Reply #3 on: July 03, 2017, 05:08:01 am »
 :)
Code: Pascal  [Select][+][-]
  1. PROGRAM Project1;
  2.  {$MODE OBJFPC}{$H+}{$J-}
  3.  
  4.  USES
  5.   Classes;
  6.  
  7.  VAR
  8.   iLine: Integer;
  9.  
  10.   Function SearchWord(str, strWord: String): Integer;
  11.     Var
  12.      sl: TStringlist;
  13.    Begin
  14.     Result:= -1;
  15.  
  16.     sl:= TStringlist.Create;
  17.      Try
  18.       sl.LoadFromFile(str);
  19.       Result:= sl.IndexOf(strWord);
  20.  
  21.       If Result > -1
  22.       Then Result:= Result+1;
  23.      Finally
  24.       sl.Free;
  25.      End;
  26.    End;
  27.  
  28.  
  29. BEGIN
  30.  iLine:= SearchWord('I:\(DOWNLOADS)\Text.txt', 'RAW');
  31.  
  32.  If iLine > -1
  33.  Then WriteLn('Found In Line ', iLine)
  34.  Else WriteLn('Not Found');
  35.  
  36.  ReadLn;
  37. END.
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

Thaddy

  • Hero Member
  • *****
  • Posts: 16937
  • Ceterum censeo Trump esse delendam
Re: Find word in textfile
« Reply #4 on: July 03, 2017, 09:04:52 am »
:)
Wrong answer!
Your code expects that the item that fully equals the string.
The code OP wanted was to find the first string that contains the search string.
That is something completely different!

And btw: his own code was already correct, he made a mistake somewhere else.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

avra

  • Hero Member
  • *****
  • Posts: 2544
    • Additional info
Re: Find word in textfile
« Reply #5 on: July 03, 2017, 11:33:45 am »
Hello, I want to check in a text file, if there is a word, then to set the whole line of the file variable
Since you simply want to check if word is in textual file loaded to sl which is TStringList, you should be able to flatten whole text as a single string and completelly avoid the loop. Something like testing Pos('hello', sl.Text)<>0.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Thaddy

  • Hero Member
  • *****
  • Posts: 16937
  • Ceterum censeo Trump esse delendam
Re: Find word in textfile
« Reply #6 on: July 03, 2017, 11:54:56 am »
No you can't because that won't return the line number. He needs that to set the whole line.....
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

avra

  • Hero Member
  • *****
  • Posts: 2544
    • Additional info
Re: Find word in textfile
« Reply #7 on: July 03, 2017, 12:58:03 pm »
that won't return the line number
In his example code line var is not needed anywhere afterwards. Only found var is relevant.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Find word in textfile
« Reply #8 on: July 03, 2017, 04:13:00 pm »
Quote
Your code expects that the item that fully equals the string.
Yes, haven't noticed that...
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Find word in textfile
« Reply #9 on: July 03, 2017, 04:19:32 pm »
Eat this  :D

Code: Pascal  [Select][+][-]
  1. PROGRAM Project1;
  2.  {$MODE OBJFPC}{$H+}{$J-}
  3.  
  4.  USES
  5.   Classes, SysUtils;
  6.  
  7.  VAR
  8.   sl: TStringlist;
  9.  
  10.   Function SearchWord(str, strWord: String; slLines: TStringlist): Boolean;
  11.     Var
  12.      sl: TStringlist;
  13.      i : Integer;
  14.    Begin
  15.     Result:= False;
  16.  
  17.     sl:= TStringlist.Create;
  18.      Try
  19.       sl.LoadFromFile(str);
  20.  
  21.       For i:= 0 To sl.Count-1
  22.       Do
  23.        Begin
  24.         If Pos(strWord, sl[i]) <> 0
  25.         Then
  26.          Begin
  27.           slLines.Add(IntToStr(i+1));
  28.           Result:= True;
  29.          End;
  30.        End;
  31.      Finally
  32.       sl.Free;
  33.      End;
  34.    End;
  35.  
  36.  
  37. BEGIN
  38.  sl:= TStringlist.Create;
  39.   Try
  40.    SearchWord('I:\(DOWNLOADS)\Text.txt', 'RAW', sl);
  41.  
  42.    If sl.Count > 0
  43.    Then WriteLn('Found In Line: '+sLineBreak+sl.Text)
  44.    Else WriteLn('Nothing Found...');
  45.   Finally
  46.    sl.Free;
  47.   End;
  48.  ReadLn;
  49. END.
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

Thaddy

  • Hero Member
  • *****
  • Posts: 16937
  • Ceterum censeo Trump esse delendam
Re: Find word in textfile
« Reply #10 on: July 03, 2017, 04:30:53 pm »
@RAW That's the same as using the in operator, which I find more elegant. Btw, not my code but his code and it did not contain an error....
« Last Edit: July 03, 2017, 04:32:26 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Find word in textfile
« Reply #11 on: July 03, 2017, 04:40:29 pm »
Yes, but now you get all the lines  :P

This looks a bit better:
Code: Pascal  [Select][+][-]
  1. BEGIN
  2.  sl:= TStringlist.Create;
  3.   Try
  4.    If SearchWord('I:\(DOWNLOADS)\Text.txt', 'Hello', sl)
  5.    Then WriteLn('Found In Line: '+sLineBreak+sl.Text)
  6.    Else WriteLn('Nothing Found...');
  7.   Finally
  8.    sl.Free;
  9.   End;
  10.  ReadLn;
  11. END.
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

Thaddy

  • Hero Member
  • *****
  • Posts: 16937
  • Ceterum censeo Trump esse delendam
Re: Find word in textfile
« Reply #12 on: July 03, 2017, 04:42:05 pm »
that won't return the line number
In his example code line var is not needed anywhere afterwards. Only found var is relevant.

Then why not simplfy it some more then?
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. uses classes,sysutils;
  3. var
  4. found : boolean;
  5. sl : TStringList;
  6. begin      
  7. found := false;
  8. sl := TStringList.Create;
  9. if paramcount > 1 then
  10. try
  11.   sl.loadfromfile(Paramstr(1));
  12.   found := sl.text.contains('Hello');  // beat that Avra ;)
  13. finally
  14.   sl.Free;
  15. end;  
  16. end.

I like all the new syntax candy.  8)
« Last Edit: July 03, 2017, 06:07:03 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Almir.Bispo

  • Jr. Member
  • **
  • Posts: 94
  • CSV Comp DB is the Best NoSQL
    • CSV Comp DB (NoSQL)
Re: Find word in textfile
« Reply #13 on: July 03, 2017, 05:58:33 pm »
Hello ! When i need works with csv tables,i use the CSV Comp DB.It can maniupale text files (csv format) to my projects
Below i show only a function to find text (full scan).It is built-in on CSV comp DB.
In CQL programming language i just use... http://adltecnologia.blogspot.com.br

Code: Pascal  [Select][+][-]
  1. {
  2.    ...\csvfile;
  3.    @algo;
  4.    (texto-to-find);
  5.    0;
  6.    0;
  7.    0;
  8.    query=0;
  9.    destino=0
  10. }
  11.  

But if wanna use only pascal can do make this:

Code: Pascal  [Select][+][-]
  1. //good for csv file
  2. function FindText(data,textfile:string):string;
  3. var I:integer;
  4.     table,resp:tstringlist;
  5.     esp,conc:string;
  6. begin
  7. table:=tstringlist.create;
  8. resp:=tstringlist.create;
  9. table.loadfromfile(textfile);
  10. resp.add(table.strings[0]); //header file
  11.   for I:=0 to table.count-1 do
  12.   begin
  13.     for esp:=1 to length(table.strings[I]) do
  14.     begin
  15.     conc:=copy(table.strings[I],esp,length(data));
  16.     if conc=data then
  17.       begin
  18.         resp.add(table.strings[I]);
  19.       end;
  20.     end;
  21.   end;
  22. result := resp.text; //result with header file
  23. resp.free;
  24. table.free;
  25. end;
  26.  

How to use:
Code: Pascal  [Select][+][-]
  1. procedure Find;
  2. begin
  3.    memo1.text := FindText('my_text','c:\somefile.csv');
  4. end
  5.  
CSV Comp DB Developer {Pascal Lover}

avra

  • Hero Member
  • *****
  • Posts: 2544
    • Additional info
Re: Find word in textfile
« Reply #14 on: July 03, 2017, 09:17:27 pm »
Then why not simplfy it some more then?
Code: Pascal  [Select][+][-]
  1.   found := sl.text.contains('Hello');  // beat that Avra ;)
No need for beating. I like helpers as long as overuse does not stay in the way of debugging.  8-)
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018