How to search for strings in a file , by keyword , using TDictionary . As it seems to me, the algorithm will work faster through TDictionary.

procedure TForm1.Button1Click(Sender: TObject); var Vhod, Vihod: TextFile; S: string; i: integer; Dictionary: TDictionary<string, integer>; begin if OpenDialog1.Execute then begin Dictionary := TDictionary<string, integer>.Create; AssignFile(Vhod, OpenDialog1.FileName); reset(Vhod); AssignFile(Vihod, ExtractFileDir(OpenDialog1.FileName) + '\svr_files.txt'); rewrite(Vihod); end; // Предполагаю что тут нужно подключить тут Dictionary := TDictionary<string, integer>.Create; while not Eof(Vhod) do begin ReadLn(Vhod, S); // Предполагаю что и тут нужно подключить if (Dictionary.ContainsKey(S)) then if (Pos('Что ищем', S) > 0) then writeln(Vihod, S); end; CloseFile(Vhod); CloseFile(Vihod); end; 
  • one
    the essence of the question is incomprehensible
  • @Igor, Well, I would like to search through the dictionary in order to speed up the work of the Pos function. - Denis
  • one
    What should this code do? - Igor
  • @Igor, Well, I enter one word in a memo or a list of words and all the lines in that text file are searched for with these keywords and written into another text file. - Denis
  • one
    may be faster, maybe not. Why do not you note the time for two options? - Igor

1 answer 1

Is there a lot of duplicate lines in the file?

 dictYes := TDictionary<string, integer>.Create; dictNo := TDictionary<string, integer>.Create; while not Eof(Vhod) do begin ReadLn(Vhod, S); if dictYes.ContainsKey(S) then begin WriteLn(Vihod, S); end else if not dictNo.ContainsKey(S) then begin if Pos('Что ищем', S) > 0 then begin dictYes.Add(S, 1); WriteLn(Vihod, S); end else begin dictNo.Add(S, 1); end; end; end; dictNo.Free; dictYes.Free; 
  • Yes, almost 45%. So there is some kind of feature here? - Denis
  • one
    @Denis If there are no identical or small lines, then the dictionary will only hurt. - Igor
  • I did as you said when opening the file: Immediately writes this message - Item not Found - Denis
  • Thanks earned, but the speed really did not increase. Essentially even diminished. In fact, this is the maximum that can be squeezed from the TDictionary? - Denis
  • one
    @ Denis Apparently - yes. - Igor