title: delphi scope of variables question
body: <p>i fill a tdictionary , read from a file, to iterate over the key-value-pairs. iterating was solved in <a href="https://stackoverflow.com/questions/5577588/delphi-dictionary-iterating">delphi dictionary iterating</a>.</p>
  
  <p>the problem is that the values in the dict are not kept, probably a scope-problem with variables. i am more used to java... the values do exist directly after assigning them to the dictionary in the procedure parsetextfile, then get lost:</p>
  
  <pre><code>program parsefile;
  
  {$APPTYPE CONSOLE}
  
  uses
    SysUtils, Classes, StrUtils, Dialogs, Generics.collections;
  
  var key : string;
      dict: TDictionary&lt;String, TStringlist&gt;;
      KeysList, Valuename: TStringList;
      KeyName: string;
      i: integer;
  
  function DeleteSpaces(str: string): string;
  var
   i: Integer;
  begin
   i:=0;
  while i&lt;=Length(str) do
    if str[i]=' ' then Delete(str, i, 1)
    else Inc(i);
    Result:=str;
  end;
  
  procedure HandleOneKey(KeyIndex:Integer; PrevKeys:string);
  var L:TStringList;
      i:Integer;
      Part: string;
      KeyName: string;
  begin
    KeyName := KeysList[KeyIndex];
    L := dict[KeyName];
    for i:=0 to L.Count-1 do
    begin
      writeln(L[i]);
      Part := KeyName + '=' + L[i];
      if KeyIndex = (KeysList.Count-1) then
        WriteLn(PrevKeys + ' ' + Part)
      else
        HandleOneKey(KeyIndex+1, PrevKeys + ' ' + Part);
    end;
  end;
  
  procedure Split(const Delimiter: Char;Input: string;const Strings: TStrings);
  begin
     Strings.Clear;
     Strings.Delimiter := Delimiter;
     Strings.DelimitedText := Input;
  end;
  
  procedure parsetestfile;
    var testfile: Textfile;
        text: string;
        splitarray: TStringList;
        subsplit1, subsplit2: TStringList;
  begin
    splitarray := TStringList.Create;
    subsplit1:= TStringList.Create;
    subsplit2:= TStringList.Create;
    AssignFile(testfile, 'g:\testfile.txt') ;
    Reset(testfile);
  
    while not Eof(testfile) do
    begin
      ReadLn(testfile, text);
      if AnsiContainsStr(text, '=') then
      begin
        Split('=', text, splitarray);
        splitarray[0] := trim(splitarray[0]);
        splitarray[1] := DeleteSpaces(splitarray[1]);
        if AnsiStartsStr('data', splitarray[0]) then
        begin
          split(' ', splitarray[0], subsplit1);
          splitarray[0]:=subsplit1[1];
          split(',', splitarray[1], subsplit2);
          dict.Add(splitarray[0], subsplit2);
          for ValueName in dict.Values do
          begin
            for i := 0 to Valuename.Count - 1 do
            write('Values are : '+ Valuename[i]);
          writeln;
          end;//for
        end;//end-data-check
      end;//end-=-check
    end;//while
    CloseFile(testfile);
    splitarray.Free;
    subsplit1.Free;
    subsplit2.Free;
  end;
  
  begin
    dict := TDictionary&lt;String, TStringlist&gt;.Create;
    parsetestfile;
    KeysList := TStringList.Create;
  
    for KeyName in dict.Keys do
      KeysList.Add(KeyName);
    for i := 0 to Keyslist.Count - 1 do
    begin
      writeln('Keylist Items: ' + Keyslist[i]);
    end;
    if KeysList.Count &gt; 0 then
    begin
      HandleOneKey(0, '');
    end;
    dict.Destroy;
    Keyslist.Free;
    WriteLn('Press ENTER to make the window go away');
    ReadLn;
  end.
  </code></pre>