Associative array

The associative array ( map english, dictionary or associative array) is a data structure, which - unlike an ordinary field ( engl. array) - non-numeric key (mostly strings) used to address the elements contained; these are in no specified order. Ideally, the keys are chosen so that a comprehensible for programmers connection between key and data value exists.

Mathematics is considered to be described by the values ​​in the associative array mapping a function with finite domain. An implementation is possible with trees, however, is by far the most common implementation of the hash table.

Programming languages ​​that support associative arrays, for example, Lua, Perl, PHP, JavaScript, Python, Ruby, Lisp, Tcl, Smalltalk, C , C #, Objective- C ( as a class in the standard library ), D, Java, Delphi ( as an array - Property), PureBasic, PostScript, GNU Bourne - again shell (version 4.0 ), PL / SQL and Visual Basic. Instead of an associative array is also called a Dictionary ( Smalltalk, Python, Objective- C, PostScript, C #), a map (C , Java, PureBasic ), a hash (Perl, Ruby ), an object ( Javascript) or a hashtable / hashmap (Java, Windows PowerShell ).

Example

The output of all the examples is " Smith". It is in each case to the same one-dimensional associative array, implemented in different languages.

Ada

With Ada.Containers.Indefinite_Hashed_Maps,       Ada.Strings.Hash,       Ada.Text_IO; procedure is Hash_Map_Example    String_String_Maps package is new Ada.Containers.Indefinite_Hashed_Maps      ( Key_type => string,       ELEMENT_TYPE => string,       Hash => Ada.Strings.Hash,       Equivalent_Keys => "=" );    Use String_String_Maps;    Person: String_String_Maps.Map; begin    Person.Insert ( " first name ", "John" );    Person.Insert ("Name ", " Smith" );    Person.Insert ( " Birthday", " 01:01:01 ");    Person.Insert ( " Location ", " model city ");    Ada.Text_IO.Put_Line ( element ( Person.Find ("Name "))); end Hash_Map_Example; C #

Var person = new System.Collections.Generic.Dictionary (); person [ " first name "] = "John"; person [ " name "] = " Smith"; person [ "Birthday "] = " 01:01:01 "; person [ " Location "] = " model city ";   System.Console.WriteLine ( person [ " name " ]); C

Std :: map person;   person [ " first name "] = "John"; person [ " name "] = " Smith"; person [ "Birthday "] = " 01:01:01 "; person [ " Location "] = " model city "; std :: cout << person [ " name "]; Common Lisp

( defparameter * person * (make -hash -table ) )   ( setf ( getHash ' first name * person *) " Hans " ) ( setf ( getHash 'name * person *) " Doe" ) ( setf ( getHash ' birthday * person *) " 01:01:01 " ) ( setf ( getHash ' place of residence * person *) " model city " )   (loop for key being the hash -keys in * person *        using ( hash -value val )        do (format t " ~ 10a => ~ a ~ % " key val ) ) D

Import std.stdio; void main ( string [ ] args ) {      string [ string] person;      person [ " first name "] = "John";      person [ " name "] = " Smith";      person [ "Birthday "] = " 01:01:01 ";      person [ " Location "] = " model city ";      writeln ( person [ " name " ]); } Haskell

Import qualified Data.Map as M - ... some action :: IO () some action = putStrLn $ person M.! "Name"   where person = M.fromList [          ( " First name ", "John" ),          ("Name ", " Smith" ),          ( " Birthday", " 01:01:01 " ),          ( "Residence", " model city " )         ] Java

Import java.util.HashMap; import java.util.Map; / / ... Map Of String String> person = new HashMap (); person.put ( " first name ", "John" ); person.put ("Name ", " Smith" ); person.put ( " Birthday", " 01:01:01 "); person.put ( " Location ", " model city "); System.out.println ( person.get ("Name ")); JavaScript

Var person = {    First name: ' Hans ',    Name: ' Doe ',    Birthday: '01 .01.01 ',    Location: ' Model City ' }; alert ( person.Name ); or

Var person = new Object (); person.Vorname = ' Hans '; person.Name = ' Smith '; person.Geburtstag = '01 .01.01 '; person.Wohnort = ' model city '; alert ( person.Name ); or

Var person = new Object (); person [ ' first name '] = ' Hans '; person [ ' name '] = ' Smith '; person [ ' birthday '] = '01 .01.01 '; person [ ' Location '] = ' model city '; alert ( person [ ' name ']); Objective- C

NSMutableDictionary * person = [ [ NSMutableDictionary alloc] init]; [person setValue: @ "John" forKey: @ " first name "]; [person setValue: @ " Doe" forKey: @ "Last Name "]; [person setObject: @ " 01:01:01 " forKey: @ " Birthday "]; [person setObject: @ " model city " forKey: @ " Location "]; NSLog ( @ "% @", [person valueForKey: @ " first name " ]); or literal Objective-C

NSDictionary * person = @ { @ " first name ": @ " Hans "                            @ "Last Name": @ " Doe",                            @ " Birthday ": @ " 01:01:01 "                            @ " Location ": @ " model city "};   NSLog ( @ "% @", person [ @ " first name " ]); Perl

My% person = ( ' first name ' = > ' Hans ',              ' Name ' = > ' Doe ',              ' Birthday ' => '01 .01.01 ',              ' Location ' = > ' Model City '); print $ person {' name '}; or

My% person; $ person {' first name '} = ' Hans '; $ person {' name '} = ' Smith '; $ person {' birthday '} = '01 .01.01 '; $ person {' Location '} = ' model city '; print $ person {' name '}; PHP

$ person = array (' first name ' = > ' Hans ',                    ' Name ' = > ' Doe ',                    ' Birthday ' => '01 .01.01 ',                    ' Location ' = > ' Model City '); echo $ person [ ' name ']; or

$ person = array (); $ person [' first name '] = ' Hans '; $ person [' name '] = ' Smith '; $ person [' birthday '] = '01 .01.01 '; $ person [' Location '] = ' model city '; echo $ person [ ' name ']; Alternative syntax in PHP 5.4:

$ person = [' first name ' = > ' Hans ',             ' Name ' = > ' Doe ',             ' Birthday ' => '01 .01.01 ',             ' Location ' = > ' model city ']; echo $ person [ ' name ']; PL / SQL

DECLARE    TYPE IS TABLE OF VARCHAR2 ty_person (50 ) INDEX BY VARCHAR2 ( 50);    person ty_person; BEGIN    person ( ' first name '): = ' Hans ';    person ( ' name '): = ' Smith ';    person ( ' birthday '): = '01 .01.01 ';    person ( ' Location '): = ' model city ';    DBMS_OUTPUT.PUT_LINE (person ( ' name ')); END; Python

Person = {" first name ": "John",             "Name": " Doe",             "Birthday", " 01:01:01 ",             "Residence", " model city "} print person [ " name " ] or

Person = { } person [ " first name "] = "John" person [ " name "] = " Doe" person [ "Birthday "] = " 01:01:01 " = person [" Location " ], " model city " print person [ " name " ] Ruby

Person = {: first name = > ' Hans ',            : Name = > ' Doe ',            : Birthday => '01 .01.01 ',            : Location = > ' model city '} puts person [: name] Tcl

Set person ( first name ) Hans set person (name ) Smith set person ( birthday) 01:01:01 set person (Residence) model city puts $ person (name ) or

Array set { person first name Hans                     Name Doe                     Birthday 01:01:01                     Location Musterstadt } puts $ person (name ) Windows PowerShell

$ person = @ { first name = ' Hans ';               Name = ' Smith ';               Birthday = '01 .01.01 ';               Location = ' model city '} $ person [' name '] Scala

Val person = map (               " First name " -> " Hans "               "Name" -> " Doe",               "Birthday " -> " 01/01/01 "               " Location " -> " Model City " )   println (person ( "Name") ) source

  • Data structure
84085
de