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

Content deleted Content added
links to articles
Line 500:
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 (programming language)|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:
 
<source lang=C>
Line 528:
LPC drivers modern enough to support a foreach() construct allow iteration over their mapping types using it.
 
=== [[Lua programming language|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 592:
</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 666:
</source>
 
=== [[OCaml]] ===
The [[OCaml]] programming language provides three different associative containers. The simplest is a list of pairs:
 
<source lang=OCaml>
Line 715:
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 [[OptimJ]] ===
{{Refimprove section|date=February 2011}}
The [[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.
 
<source lang=Java>
Line 752:
</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.
 
A hash variable is marked by a <code>%</code> [[sigil (computer programming)|sigil]], to distinguish it from scalar, array and other data types. A hash literal is a key-value list, with the preferred form using Perl's <code>=&gt;</code> token, which is mostly semantically identical to the comma and makes the key-value association clearer:
Line 807:
</source>
 
=== [[PHP]] ===
[[PHP]]'s built-in array type is in reality an associative array. Even when using numerical indexes, PHP internally stores it as an associative array.<ref>About the implementation of [http://se.php.net/manual/en/language.types.array.php Arrays] in PHP</ref> This is why one in PHP can have non-consecutive numerically indexed arrays. The keys have to be integer or string (floating point numbers are truncated to integer), while values can be of arbitrary types, including other arrays and objects. The arrays are heterogeneous; a single array can have keys of different types. PHP's associative arrays can be used to represent trees, lists, stacks, queues and other common data structures not built into PHP.
 
An associative array can be declared using the following syntax:
Line 850:
If you want an associative array that can use objects as keys instead of strings and integers, you can use the [http://php.net/SplObjectStorage SplObjectStorage] class from the Standard PHP Library (SPL).
 
=== [[Pike (programming language)|Pike]] ===
[[Pike (programming language)|Pike]] has built-in support for Associative Arrays, which are referred to as mappings. Mappings are created as follows:
 
<source lang=Pike>
Line 887:
</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:
 
<code>
Line 949:
MyDict /rouge undef</code>
 
=== [[Python (programming language)|Python]] ===
In [[Python (programming language)|Python]], associative arrays are called ''[[Python syntax and semantics#Collection types|dictionaries]]''. Dictionary literals are marked with curly braces:
 
<source lang=Python>
Line 996:
</source>
 
=== [[REXX]] ===
In [[REXX]], associative arrays are called ''Stem variables'' or ''Compound variables''.
 
<source lang=REXX>
Line 1,018:
</source>
 
[[REXX]] has no easy way of automatically accessing the keys for a stem variable and typically the
keys are stored in a separate associative array with numeric keys.
 
=== [[Ruby programming language|Ruby]] ===
 
In [[Ruby programming language|Ruby]] a [[hash table|hash]] is used as follows:
 
<source lang=Ruby>
Line 1,048:
</source>
 
=== [[S-Lang (programming language)|S-Lang]] ===
[[S-Lang (programming language)|S-Lang]] has an associative array type.
 
For example:
Line 1,079:
</source>
 
=== [[Scala (programming language)|Scala]] ===
[[Scala (programming language)|Scala]] provides an immutable Map class as part of the scala.collection framework:
 
<source lang=Scala>
Line 1,096:
This returns an Option type, Scala's equivalent of a [[Monad_(functional_programming)#The Maybe_monad|the Maybe monad]] in Haskell.
 
=== [[Smalltalk]] ===
In [[Smalltalk]] a dictionary is used:
 
<source lang=Smalltalk>
Line 1,120:
[[Garbage collection (computer science)#Strong and Weak references|strong references]] to both key and value. Variants exist which hash/compare on identity (IdentityDictionary) or keep [[Garbage collection (computer science)#Strong and Weak references|weak references]] (WeakKeyDictionary / WeakValueDictionary).
 
=== [[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,184:
 
 
=== [[Tcl]] ===
 
There are two [[Tcl]] facilities that support associative array semantics. An
array is a collection of ''variables''. A '''dict''' is a full implementation
of associative arrays.
Line 1,256:
</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):
 
<source lang=VB>
Line 1,282:
</source>
 
=== [[Windows PowerShell|PowerShell]] ===
Unlike many other [[command line interpreter]]s, [[Windows PowerShell|PowerShell]] has built-in, language-level support for defining associative arrays.
 
For example:
Line 1,371:
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,381:
</source>
 
=== [[YAML]] ===
 
<source lang=YAML>