-1

I want to have a text file with a list of strings (say for example comma separated with key and values that I can use for auto replacement) and store each key-value pair in a TDictionary<string, string>. How do I populate the dictionary?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
user1817376
  • 107
  • 11

2 Answers2

4

From your comment it seems you want to know how to pull out some key value pairs, comma separated, into a dictionary. Here's a basic example:

procedure PopulateKeyValueDict(Strings: TStrings;
  Dict: TDictionary<string, string>);
var
  CommaPos: Integer;
  Line: string;
  Key, Value: string;
begin
  for Line in Strings do
  begin
    CommaPos := Pos(',', Line);
    if CommaPos=0 then
      raise Exception.CreateFmt(
        'Could find comma separated key/value pair in ''%s''',
        [Line]
      );
    Key := Copy(Line, 1, CommaPos-1);
    Value := Copy(Line, CommaPos+1, MaxInt);
    Dict.Add(Key, Value);
  end;
end;

You may likely want to add more error-checking and so on, but I'm assuming you already know how to do that. This example illustrates splitting a line on the first comma, and also how to populate a dictionary.

In order to use it you need to transfer your file to a TStrings object. That's routine:

var
  Strings: TStringList;
....
Strings := TStringList.Create;
try
  Strings.LoadFromFile(FileName);
  PopulateKeyValueDict(Strings, Dict);
finally
  Strings.Free;
end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • How do I use it for text replacements? – user1817376 Dec 21 '12 at 09:27
  • 1
    It's meant to be one question at a time here on SO. But you need to iterate through the dictionary and for each pair, do your string replace. You can use the `StringReplace` function for that. – David Heffernan Dec 21 '12 at 09:31
  • @David perhaps he can do StringReplace over StringList.Text or StringList.CommaText between loading the file and populating the dictionary – Arioch 'The Dec 22 '12 at 10:07
1

If you only have one-to-one key-value relation (not like three key words "apple" and "apples" and "McIntoshes" would be turned into "McIntosh") - then probably the simpliest way would be to use TStringList, providing that

  1. U make your file exactly of Key=Value lines, not Key,Value
  2. U either need it case sensitive or do UpperCase over the file.

Then u use http://docwiki.embarcadero.com/Libraries/XE3/en/System.Classes.TStrings.Values

To speed things up a bit you can use THashedStringList of IniFiles unit. There also was somethign similar in JCL in JclXML unit.

Arioch 'The
  • 15,799
  • 35
  • 62
  • That is awesome approach as well but I will have more than one key for a value. Thanks for your suggestion! – user1817376 Dec 21 '12 at 13:55
  • 2
    @user1817376 if this is `awesome` but didn't answer your question, why did you accept it? IMHO David's answer fits perfect your question and should be accepted. BTW with a TStringList you can have many keys with the same value like TDictionary does. Only Keys have to be unique – Sir Rufo Dec 22 '12 at 08:24
  • 1
    I agree with @SirRufo on this. When answer is awesome - it is acknowledged by upvoting it. But whether answer is correct or not - that is a separate issue. I believe that - if this answer is out of context you'd better do 1: fix your question by explicitly spelling the requirements, that make my answer incorrect; 2: accept the answer that really applies to your situation. – Arioch 'The Dec 22 '12 at 10:04