Associative array in Delphi , array with string key is possible?
Asked Answered
E

4

8

If you work with php you can see the php have associative array (or array width string key) in programing lang. For example:

$server['hostname']  =  'localhost';
$server['database']  =  'test';
$server['username']  =  'root';
$server['password']  =  'password' ;    

// 2d array
$all['myserver']['hostname'] = 'localhost' ;

But can't find any default way to use associative array in delphi.

First I want find default way with out any output component or class . Second if really I cant find with internal way I force choose output classes only.

I use Delphi XE3 , many thanks for your help.

edit: I found one class here : http://www.delphipages.com/forum/showthread.php?t=26334 same as php , but any better way?

Edithe answered 7/12, 2012 at 15:0 Comment(2)
A very nice implementation of Associative Array in Delphi can be found here: https://mcmap.net/q/1321577/-change-the-variable-of-which-the-name-is-given-as-input-pascal . It is closest match to PHP's Assoc ArraySleepless
@iPath: agree with thanks : )Edithe
I
14

You can use tStrings and tStringList for this purpose, but 2d arrays are out of the scope of these components.

Usage;

var
  names  : TStrings;
begin
  ...
  names := TStringList.Create;
  ...
  ...
  names.values['ABC'] := 'VALUE of ABC' ;
  ...
  ...
end ;
Inflexion answered 7/12, 2012 at 15:6 Comment(1)
should be := (the assignment operator) not = which compares two values/ objectsIncluded
L
19

You can use TDictionary<string,string> from the Generics.Collections unit.

var
  Dict: TDictionary<string,string>;
  myValue: string;
....
Dict := TDictionary<string,string>.Create;
try
  Dict.Add('hostname', 'localhost');
  Dict.Add('database', 'test');
  //etc.
  myValue := Dict['hostname'];
finally
  Dict.Free;
end;

And so on and so on.

If you want a dictionary that contains a dictionary, you can do use TDictionary<string, TDictionary<string,string>>.

However, when you do that you'll need to take special care over the lifetime of the dictionary items that are contained in the outer dictionary. You can use TObjectDictionary<K,V> to help manage that for you. You'd create one of these objects like this:

TObjectDictionary<string, TDictionary<string,string>>.Create([doOwnsValues]);

This TObjectDictionary<k,V> operates the same was as a traditional TObjectList with OwnsObjects set to True.

Lovelovebird answered 7/12, 2012 at 15:4 Comment(7)
here is not complete way i found better in here : delphipages.com/forum/showthread.php?t=26334 , but i want complete class , but Thank you David :)Edithe
I don't understand that comment. What you are looking for are the classes described in my answer.Lovelovebird
tDictionary was introduced in newer Delphi versions. This solution is far better than the one that I gave with tStrings. Because the implementation of tDictionary is better than tStrings.Rameriz
I envy those of you using Delphi 2009 onwards.... I am forced to used TStringListIncluded
@MaxCarroll Yes, that's not very much fun at all!Lovelovebird
is there a way to get keys too ? for example Dict.key[0] ?!Geneticist
@peiman the rtl dictionary has Keys and Values properties that expose the keys and values. But the rtl dictionary is not ordered, so acessing by integer index is not possible. For that you need the spring4d dictionary.Lovelovebird
I
14

You can use tStrings and tStringList for this purpose, but 2d arrays are out of the scope of these components.

Usage;

var
  names  : TStrings;
begin
  ...
  names := TStringList.Create;
  ...
  ...
  names.values['ABC'] := 'VALUE of ABC' ;
  ...
  ...
end ;
Inflexion answered 7/12, 2012 at 15:6 Comment(1)
should be := (the assignment operator) not = which compares two values/ objectsIncluded
P
0

I had solved the problem that simple way (example):

uses StrUtils;

...

const const_TypesChar : array [0..4] of String =
    (
      'I',
      'F',
      'D',
      'S',
      'B'
    );
const const_TypesStr : array [0..4] of String =
    (
      'Integer',
      'Float',
      'Datetime',
      'String',
      'Boolean'
    );

...

Value := const_TypesStr[ AnsiIndexStr('S', const_TypesChar) ];

// As an example, after execution of this code Value variable will have 'String' value.

//

Then in program we are using two arrays const_TypesChar and const_TypesStr as one associative array with AnsiIndexStr function.

The plus is that it's simple and that we don't need to change code in different places in program every time when we add elements to our arrays.

Pamilapammi answered 5/5, 2016 at 11:49 Comment(0)
H
0

Look at ArrayS. You can use associative arrays which stores predefined type of data (integer, string, boolean, float), or any of them. For example, below I define an associative array of floats:

uses ArrayS;

var floats : IFltArray;

floats := CreateArray;
floats['first'] := 0.1;
floats['second'] := 0.2;
writeln( floats['second'] );

And so on.

Updated at 2020-03-15

Zipped source code

Ussage example in Russian

Halide answered 13/8, 2018 at 21:38 Comment(2)
404 unfortunatelyBeker
The zipped source code and ussage example in Russian.Halide

© 2022 - 2024 — McMap. All rights reserved.