Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's some very basic code that should get you on your way. There's no error checking, and you'll no doubt want to develop it and modify it further. The point is that there should be some ideas to help you write code that works for you.</p> <p>Now that I have comma-separated the fields, but made no attempt to handle the appearance of commas in any of the values. If this is a problem then choose a different delimiter, or escape the commas. I had toyed with writing each field on its own line (effectively using a newline as the separator), but this makes the reading code more tricky to write.</p> <p>Again, the main point is that this is not final production code, but is intended to give you a starting point.</p> <pre><code>function Split(const s: string; Separator: char): TStringDynArray; var i, ItemIndex: Integer; len: Integer; SeparatorCount: Integer; Start: Integer; begin len := Length(s); if len=0 then begin Result := nil; exit; end; SeparatorCount := 0; for i := 1 to len do begin if s[i]=Separator then begin inc(SeparatorCount); end; end; SetLength(Result, SeparatorCount+1); ItemIndex := 0; Start := 1; for i := 1 to len do begin if s[i]=Separator then begin Result[ItemIndex] := Copy(s, Start, i-Start); inc(ItemIndex); Start := i+1; end; end; Result[ItemIndex] := Copy(s, Start, len-Start+1); end; type TValue = record i1, i2: Integer; s: string; end; TMyDict = class(TDictionary&lt;string,TValue&gt;) public procedure SaveToFile(const FileName: string); procedure LoadFromFile(const FileName: string); end; { TMyDict } procedure TMyDict.SaveToFile(const FileName: string); var Strings: TStringList; Item: TPair&lt;string,TValue&gt;; begin Strings := TStringList.Create; Try for Item in Self do begin Strings.Add(Format( '%s,%s,%d,%d', [Item.Key, Item.Value.s, Item.Value.i1, Item.Value.i2] )); end; Strings.SaveToFile(FileName); Finally FreeAndNil(Strings); End; end; procedure TMyDict.LoadFromFile(const FileName: string); var Strings: TStringList; Item: TPair&lt;string,TValue&gt;; Line: string; Fields: TStringDynArray; begin Strings := TStringList.Create; Try Strings.LoadFromFile(FileName); for Line in Strings do begin Fields := Split(Line, ','); Assert(Length(Fields)=4); Item.Key := Fields[0]; Item.Value.s := Fields[1]; Item.Value.i1 := StrToInt(Fields[2]); Item.Value.i2 := StrToInt(Fields[3]); Add(Item.Key, Item.Value); end; Finally FreeAndNil(Strings); End; end; </code></pre> <p>Note that you don't attempt to search the file on disk. You simply load it into memory, into the dictionary and look things up from there.</p> <p>A dictionary is great when you always use the same key. If you have multiple keys then a dictionary is less convenient, but who cares about the performance impact if you've only got 15 records?!</p> <p><strong>Disclaimer</strong>: I've not run the code, I've not tested it, etc. etc.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload