g'morning!
i fill a dictionary TDictionary<String, TStringlist>
(delphi-collections-unit) with strings as values and several strings as values.
something like:
- names = john, lisa, stan
- skills = read, write, speak
- ages = 12, 14, 16
(without "," of course). what i need is to iterate this dictionary and to multiply out the values with the keys. output should be like
- names = john skills = read ages = 12
- names = john skills = read ages = 14
- names = john skills = read ages = 16
- names = john skills = write ages = 12
- names = john skills = write ages = 14
- names = john skills = write ages = 16
- ...
- names = lisa skills = read ages = 12
- ...
- names = stan skills = speak ages = 16
so every combination. how can i do so? the number of keys is dynamic and so is the size of the tstringlist. thanks! SOLVED by now...
now the problem with the scope. following is the procedure that fills the dict. the subsplits and the splitstring are stringlists, that get freed at the end of the procedure. the dict is created after the procedures-block (in main? how is it called?), the fill-method is called and then i want to do recursion like in the code-example but there are no values in the dict....
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;//
end;//
end;//
end;//
What you want is made a bit complicated by the use of the
TDictionary<string, TStringList>
, because that implies variable number of keys. If it weren't for the variable number of keys, you wouldn't need a dictionary and you'd simply iterate over 3 TStringLists.That said, you've got the classic "generate all permutations" problem. It can be solved using recursion or backtracking. Recursion is simpler to implement, backtracking uses less stack space. The choice is yours. Here's a complete console application that does the whole deal, from initializing the dictionary, populating the dictionary, generating all permutations using a recursive function.