Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associative array in Delphi , array with string key is possible?

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?

like image 865
A1Gard Avatar asked Dec 07 '12 15:12

A1Gard


People also ask

Which key is used in associative array?

Traversing the Associative Array Example: In Associative arrays in PHP, the array keys() function is used to find indices with names provided to them, and the count() function is used to count the number of indices.

Can associative array have same key?

No, you cannot have multiple of the same key in an associative array. You could, however, have unique keys each of whose corresponding values are arrays, and those arrays have multiple elements for each key.

How do you change the key in an associative array?

Just make a note of the old value, use unset to remove it from the array then add it with the new key and the old value pair. Save this answer.

How can you access the elements of an associative array?

The elements of an associative array can only be accessed by the corresponding keys. As there is not strict indexing between the keys, accessing the elements normally by integer index is not possible in PHP. Although the array_keys() function can be used to get an indexed array of keys for an associative array.


2 Answers

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.

like image 93
David Heffernan Avatar answered Sep 28 '22 07:09

David Heffernan


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 ;
like image 45
Ali Avcı Avatar answered Sep 28 '22 08:09

Ali Avcı