Assignment table (data structure)

from Wikipedia, the free encyclopedia

The associative data field is a data structure in which, unlike a normal field , non-numeric (or discontinuous) keys , mostly character strings , can also be used to address the elements it contains; these are not saved in any specific order. Ideally, the keys are chosen in such a way that the programmer can understand the connection between the key and the data value.

From a mathematical point of view, the value assignments in the associative array describe a mapping with a finite set of definitions and a finite set of images . An implementation is possible with trees , but by far the most common implementation is the hash table .

Programming languages ​​that support associative fields are for example Lua , Perl , PHP , JavaScript , Python , Ruby , LISP , Tcl , awk , Smalltalk , C ++ , C # , Objective-C (as a class of the standard library), D , Java , Delphi ( as array property), PureBasic , PostScript , Bash (from version 4.0), PL / SQL and Visual Basic . Instead of an associative array, one also speaks of a dictionary (Smalltalk, Python, Objective-C, PostScript, C #), a map (C ++, Java, PureBasic), a hash (Perl, Ruby), an object (JavaScript / JSON ) , a Table (Lua), a dynamic array or Dynarray ( PAL ), a Hashtable / Hashmap (Java, Windows PowerShell ) or a Compound Variable ( REXX ).

example

The output of all examples is "Mustermann". It is always the same one-dimensional associative field, implemented in different languages. Sometimes there are other options in the languages ​​to create an associative data field.

C #

var person = new System.Collections.Generic.Dictionary<string, string>();
person["Vorname"]    = "Hans";
person["Name"]       = "Mustermann";
person["Geburtstag"] = "01.01.01";
person["Wohnort"]    = "Musterstadt";

System.Console.WriteLine(person["Name"]);

or, since C # 3 with Collection Initializer:

using System;
using System.Collections.Generic;

var person = new Dictionary<string, string>() {
    { "Vorname", "Hans" },
    { "Name", "Mustermann" },
    { "Geburtstag", "01.01.01" },
    { "Wohnort", "Musterstadt" }
};

Console.WriteLine(person["Name"]);

Common Lisp

(defparameter *person* (make-hash-table))

(setf (gethash 'vorname    *person*) "Hans")
(setf (gethash 'name       *person*) "Mustermann")
(setf (gethash 'geburtstag *person*) "01.01.01")
(setf (gethash 'wohnort    *person*) "Musterstadt")

(loop for key being the hash-keys in *person*
      using (hash-value val)
      do (format t "~10a => ~a~%" key val))

Haskell

import qualified Data.Map as M
-- ...
someAction :: IO ()
someAction = putStrLn $ person M.! "Name"
    where person = M.fromList [
        ("Vorname", "Hans"),
        ("Name", "Mustermann"),
        ("Geburtstag", "01.01.01"),
        ("Wohnort", "Musterstadt")
    ]

Objective-C

NSMutableDictionary *person = [[NSMutableDictionary alloc] init];
[person setValue:@"Hans" forKey:@"Vorname"];
[person setValue:@"Mustermann" forKey:@"Nachname"];
[person setObject:@"01.01.01" forKey:@"Geburtstag"];
[person setObject:@"Musterstadt" forKey:@"Wohnort"];
NSLog(@"%@", [person valueForKey:@"Vorname"]);

Pearl

%person = ('Vorname', 'Hans', 'Name', 'Mustermann', 'Geburtstag', '01.01.01', 'Wohnort', 'Musterstadt');
print $person{'Name'};

PL / SQL

DECLARE
    TYPE ty_person IS TABLE OF VARCHAR2(50) INDEX BY VARCHAR2(50);
    person  ty_person;
BEGIN
    person('Vorname')    := 'Hans';
    person('Name')       := 'Mustermann';
    person('Geburtstag') := '01.01.01';
    person('Wohnort')    := 'Musterstadt';
    DBMS_OUTPUT.PUT_LINE(person('Name'));
END;

Tcl

array set person {
    Vorname    Hans
    Name       Mustermann
    Geburtstag 01.01.01
    Wohnort    Musterstadt
}
puts $person(Name)

literature

  • Peter A. Henning, Holger Vogelsang (ed.): Paperback programming languages . 2nd Edition. Hanser, Munich 2007, ISBN 978-3-446-40744-2 .

Individual evidence