Comparison of programming languages (associative array): Difference between revisions

Content deleted Content added
No edit summary
SmackBot (talk | contribs)
m Date maintenance tags and general fixes: build 545:
Line 1:
{{ProgLangCompare}}
 
== Language support ==
{{Expand|date=January 2007}}
 
The following is a comparison of associative arrays (also "mapping", "hash", and "dictionary") in various programming languages. For more details, see [[Associative array]].
 
=== Awk ===
[[Awk]] has built-in, language-level support for associative arrays.
 
For example:
Line 60 ⟶ 61:
Another 3rd party library, [http://uthash.sourceforge.net/ uthash], also creates associative arrays from C structures. A structure represents a value, and one of the structure fields acts as the key.
 
Finally, the [[Glib]] library also supports associative arrays, along with many other advanced data types and is the recommended implementation of the GNU Project{{FactCitation needed|date=December 2008}}.
 
=== ColdFusion ===
You can use a [[ColdFusion]] structure to perform as an associative array. Here is a sample in ColdFusion:
 
Line 74 ⟶ 75:
phoneBook.UnknownComic = "???";
</cfscript>
 
 
<cfoutput>
Line 86:
 
</cfoutput>
 
 
</source>
 
=== C# ===
 
<source lang="csharp">
Hashtable ht = new Hashtable();
Line 112 ⟶ 110:
</source>
 
=== C++ ===
[[C++]] also has a form of associative array called <code>std::map</code> (see [[Standard Template Library#Containers]]). One could create a map with the same information as above using C++ with the following code:
 
Line 138 ⟶ 136:
In C++, the <code>std::map</code> class is [[Generic programming#Templates in C.2B.2B|templated]] which allows the [[data type]]s of keys and values to be different for different <code>map</code> instances. For a given instance of the <code>map</code> class the keys must be of the same base type. The same must be true for all of the values. Although <code>std::map</code> is typically implemented using a [[self-balancing binary search tree]], C++'s [[Technical Report 1]] (TR1) defines a second map called <code>[[std::tr1::unordered map]]</code> with the algorithmic characteristics of a hash table. This is a common vendor extension to the STL as well, usually called <code>hash_map</code>, being available from such implementations as SGI and STLPort.
 
=== Cocoa/GNUstep (Objective-C) ===
[[Cocoa (API)]] and [[GNUstep]] handle associative arrays using <code>NSMutableDictionary</code> (a mutable version of <code>NSDictionary</code>) class cluster. This class allows assignments between any two objects to be made. A copy of the key object is made before it is inserted into <code>NSMutableDictionary</code>, therefore the keys must conform to the <code>NSCopying</code> protocol. When being inserted to a dictionary, the value object receives a retain message to increase its reference count. The value object will receive the release message when it will be deleted from the dictionary (both explicitly or by adding to the dictionary a different object with the same key).
 
Line 196 ⟶ 194:
=== D ===
[[D programming language|D]] offers direct support for associative arrays
in the core language - they are implemented as a chaining hash table with binary trees<ref>{{citeCite web |url=http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=30153 |title=D Newsletter |accessdate=2008-06-15 |work= |date= }}</ref>. The equivalent example would be:
 
<source lang="d">
Line 225 ⟶ 223:
</source>
 
=== Delphi ===
[[Borland Delphi|Delphi]] does not offer direct support for associative arrays. However, you can simulate associative arrays using TStrings object. Here's an example:
 
Line 254 ⟶ 252:
 
===Erlang===
 
Erlang offers many approaches to represent mappings, two of the most common in the standard library are keylists and dictionaries.
 
Line 297 ⟶ 294:
In both cases, any Erlang term can be used as the key. Variations include the <code>orddict</code> module, implementing ordered dictionaries, and <code>gb_trees</code>, implementing general balanced trees.
 
=== FoxPro ===
[[Visual FoxPro]] implements mapping with the Collection Class.
 
Line 312 ⟶ 309:
See Collection in FoxPro Help for all the details.
 
=== Go ===
[[Go (programming language)|Go]] has built-in, language-level support for associative arrays, called ''maps''. A map's key type may only be a boolean, numeric, string, pointer, function, interface, map, or channel type. A map type is written like this: <tt>map[keytype]valuetype</tt>.
 
Line 345 ⟶ 342:
</pre>
 
=== Haskell ===
The [[Haskell (programming language)|Haskell]] programming language's report only provides one kind of associative container: a list of pairs:
 
Line 393 ⟶ 390:
Lists of pairs and functional maps both provide a purely functional interface, which is more idiomatic in Haskell. In contrast, hash tables provide an imperative interface in the IO monad.
 
=== Java ===
In [[Java (programming language)|Java]] associative arrays are implemented as "maps"; they are part of the [[Java collections framework]]. Since [[Java Platform, Standard Edition|J2SE]] 5.0 and the introduction of [[generic programming|generics]] into Java, collections can have a type specified; for example, an associative array mapping strings to strings might be specified as follows:
 
Line 421 ⟶ 418:
Analagously, TreeMap, and other sorted data structures, requires that an ordering be defined on the data type. Either the data type must already have defined its own ordering, by implementing the {{Javadoc:SE|java/lang|Comparable}} interface; or a custom {{Javadoc:SE|java/util|Comparator}} must be provided at the time the map is constructed. Like with HashMap above, the relative ordering of keys in a TreeMap should not change once they have been inserted into the map.
 
=== JavaScript ===
JavaScript (and its standardized version: [[ECMAScript]]) is a [[Prototype-based programming|prototype-based]] [[Object-oriented programming|object-oriented]] language. In JavaScript an object is a mapping from property names to values -- thatvalues—that is, an associative array with one caveat: since property names are strings, only string and (coerced) integer keys are allowed. Other than that difference, objects also include one feature unrelated to associative arrays: a prototype link to the object they inherit from. Doing a lookup for a property will forward the lookup to the prototype if the object does not define the property itself.
 
An object literal is written as <code>{ property1 : value1, property2 : value2, ... }</code>. For example:
Line 482 ⟶ 479:
See [http://blog.metawrap.com/blog/June6thIsJavaScriptArrayAndObjectprototypeAwarenessDay.aspx JavaScript Array And Object Prototype Awareness Day] for more information on the issue.
 
=== KornShell 93 (and compliant shells: ksh93, zsh, ...) ===
 
<!-- Tested in ksh93 r -->
:'''Note:''' ''The latest version of [[Bourne-Again shell|bash]], 3.2, doesn't support associative arrays properly yet.''
Line 496 ⟶ 492:
${phonebook["John Doe"]};
 
=== Lisp ===
[[Lisp programming language|Lisp]] was originally conceived as a "LISt Processing" language, and one of its most important data types is the linked list, which can be treated as an association list ("alist").
 
Line 511 ⟶ 507:
It is easy to construct composite abstract data types in Lisp, using structures and/or the object-oriented programming features, in conjunction with lists, arrays, and hash tables.
 
=== LPC ===
 
[[LPC (programming language)|LPC]] implements associative arrays as a fundamental type known as either map or mapping, depending on the driver. The keys and values can be of any type. A mapping literal is written as <code>([ key_1 : value_1, key_2 : value_2 ])</code>. Procedural use looks like:
 
Line 540 ⟶ 535:
LPC drivers modern enough to support a foreach() construct allow iteration over their mapping types using it.
 
=== Lua ===
In [[Lua programming language|Lua]], table is a fundamental type that can be used either as array (numerical index, fast) or as associative array.
The keys and values can be of any type, except nil. The following focuses on non-numerical indexes.
Line 604 ⟶ 599:
</source>
 
=== MUMPS ===
In [[MUMPS]] every array is an associative array. The built-in, language-level, direct support for associative arrays
applies to private, process-specific arrays stored in memory called "locals" as well as to the permanent, shared arrays stored on disk which are available concurrently by multiple jobs. The name for globals is preceded by the circumflex "^" to distinguish it from local variable names.
Line 619 ⟶ 614:
You can also loop through an associated array as follows:
 
SET NAME=""
FOR S NAME=$ORDER(^phonebook(NAME)) QUIT:NAME="" WRITE NAME," Phone Number :",^phonebook(NAME),!
 
=== OCaml ===
 
 
=== OCaml ===
The [[OCaml]] programming language provides three different associative containers. The simplest is a list of pairs:
 
Line 673 ⟶ 666:
Lists of pairs and functional maps both provide a purely functional interface. In contrast, hash tables provide an imperative interface. For many operations, hash tables are significantly faster than lists of pairs and functional maps.
 
=== Optimj ===
The [http://www.ateji.com/optimj.html Optimj] programming language is an extension of Java 5. As java, Optimj provides maps. But, OptimJ also provides true associative arrays: java arrays are indexed with 0-based integers; associative arrays are indexed with any collection of keys.
 
Line 709 ⟶ 702:
</source>
 
=== Perl ===
[[Perl]] has built-in, language-level support for associative arrays. Modern Perl vernacular refers to associative arrays as ''hashes''; the term ''associative array'' is found in older documentation, but is considered somewhat archaic. Perl hashes are flat: keys are strings and values are scalars. However, values may be [[reference (computer science)|references]] to arrays or other hashes, and the standard Perl module Tie::RefHash enables hashes to be used with reference keys.
 
Line 808 ⟶ 801:
If you want an associative array that can use objects as keys instead of strings and integers, you can use the [http://www.php.net/manual/en/class.splobjectstorage.php SplObjectStorage] class from the Standard PHP Library (SPL).
 
=== Pike ===
[[Pike (programming language)|Pike]] has built-in support for Associative Arrays, which are referred to as mappings. Mappings are created as follows:
 
Line 845 ⟶ 838:
</source>
 
=== PostScript ===
In [[PostScript]], associative arrays are called dictionaries. In Level 1 PostScript they must be created explicitly, but Level 2 introduced direct declaration using the double-brace syntax:
 
Line 855 ⟶ 848:
/blue (bleu) def
end
 
% Level 2 declaration
<<
Line 862 ⟶ 855:
/blue (blau)
>>
 
% Both methods leave the dictionary on the operand stack</code>
 
Line 870 ⟶ 863:
% With the previous two dictionaries still on the operand stack
/red get print % outputs 'rot'
 
begin
green print % outputs 'vert'
Line 897 ⟶ 890:
/vert (gruen)
>> def
 
% add to it
MyDict /bleu (blue) put
 
% change it
MyDict /vert (green) put
 
% remove something
MyDict /rouge undef</code>
 
=== Python ===
In [[Python (programming language)|Python]], associative arrays are called ''[[Python syntax and semantics#Collection types|dictionaries]]''. Dictionary literals are marked with curly braces:
 
Line 918 ⟶ 911:
</source>
 
To access an entry in Python simply use the array indexing operator. For example, the expression <code>phonebook['Sally Smart']</code> would return <code>'555-9999'</code>.
 
An example loop [[iterator#Python|iterating]] through all the keys of the dictionary:
Line 954 ⟶ 947:
</source>
 
=== REXX ===
In [[REXX]], associative arrays are called ''Stem variables'' or ''Compound variables''.
 
Line 1,008 ⟶ 1,001:
</source>
 
=== S-Lang ===
[[S-Lang (programming language)|S-Lang]] has an associative array type.
 
Line 1,039 ⟶ 1,032:
</source>
 
=== Smalltalk ===
In [[Smalltalk]] a dictionary is used:
 
Line 1,061 ⟶ 1,054:
</source>
 
=== SNOBOL ===
[[SNOBOL]] is one of the first (if not the first) programming languages to use associative arrays.
Associative arrays in [[SNOBOL]] are called Tables.
Line 1,071 ⟶ 1,064:
PHONEBOOK['J. Random Hacker'] = '553-1337'
</source>
 
=== Standard ML ===
The SML'97 standard of the [[Standard ML]] programming language does not provide any associative containers. However, various implementations of Standard ML do provide associative containers.
 
Line 1,123 ⟶ 1,117:
Another Standard ML implementation, [[Moscow ML]], also provides some associative containers. First, it provides polymorphic hash tables in the <code>Polyhash</code> structure. Also, some functional maps from the SML/NJ library above are available as <code>Binarymap</code>, <code>Splaymap</code>, and <code>Intmap</code> structures.
 
=== Tcl ===
In [[Tcl]] every array is an associative array. As of Tcl 8.5, there is also the '''dict''' command that operates on dictionary ''values''; arrays are always ''variables''.
 
Line 1,169 ⟶ 1,163:
</source>
 
=== Visual Basic ===
 
There is no standard implementation common to all dialects. [[Visual Basic]] can use the Dictionary class from the [[Windows Scripting Host|Microsoft Scripting Runtime]] (which is shipped with Visual Basic 6):
 
Line 1,196 ⟶ 1,189:
</source>
 
=== Windows PowerShell ===
 
Unlike many other [[command line interpreter]]s, [[Windows PowerShell|PowerShell]] has built-in, language-level support for defining associative arrays.
 
Line 1,280 ⟶ 1,272:
$hash3 = $hash1 + $hash2
</source>
 
== Data serialization formats support ==
{{Expand}}
{{Expand|date=September 2010}}
 
Many data serialization formats also support associative arrays (see [[Comparison of data serialization formats#Syntax comparison of human-readable formats|this table]])
 
=== [[JSON]] ===
Keys can only be strings
<source lang="javascript">
Line 1,295 ⟶ 1,288:
</source>
 
=== [[YAML]] ===
<source lang="text">
Sally Smart: 555-9999
Line 1,302 ⟶ 1,295:
</source>
 
== References ==
<references/>
 
{{DEFAULTSORT:Comparison Of Programming Languages (Mapping)}}
[[Category:Associative arrays]]