ArticleslgStudy

science

Comparison of programming languages (associative array)

Comparison of programming languages (associative array) is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Comparison of programming languages (associative array) rather than just read about it. In short: This comparison of programming languages (associative arrays) compares the features of associative array data structures or array-lookup processing for over 40 computer programming languages. Language support The following is a comparison of associative arrays (also "mapping", "hash", and "dictionary") in various programming languages.

Key takeaways

  • Comparison of programming languages (associative array) belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Comparison of programming languages (associative array) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Comparison of programming languages (associative array) from memory before moving on to harder problems.

Reference excerpt

This comparison of programming languages (associative arrays) compares the features of associative array data structures or array-lookup processing for over 40 computer programming languages.

Language support

The following is a comparison of associative arrays (also "mapping", "hash", and "dictionary") in various programming languages.

AWK AWK has built-in, language-level support for associative arrays. For example:

The following code loops through an associated array and prints its contents:

The user can search for elements in an associative array, and delete elements from the array. The following shows how multi-dimensional associative arrays can be simulated in standard AWK using concatenation and the built-in string-separator variable SUBSEP:

C There is no standard implementation of associative arrays in C, but a 3rd-party library, C Hash Table, with BSD license, is available. Another 3rd-party library, uthash, also creates associative arrays from C structures. A structure represents a value, and one of the structure fields serves 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. Similar to GLib, Apple's cross-platform Core Foundation framework provides several basic data types. In particular, there are reference-counted CFDictionary and CFMutableDictionary.

C# C# uses the collection classes provided by the .NET Framework. The most commonly used associative array type is System.Collections.Generic.Dictionary<TKey, TValue>, which is implemented as a mutable hash table. The relatively new System.Collections.Immutable package, available in .NET Framework versions 4.5 and above, and in all versions of .NET Core, also includes the System.Collections.Immutable.Dictionary<TKey, TValue> type, which is implemented using an AVL tree. The methods that would normally mutate the object in-place instead return a new object that represents the state of the original object after mutation.

Creation The following demonstrates three means of populating a mutable dictionary:

the Add method, which adds a key and value and throws an exception if the key already exists in the dictionary; assigning to the indexer, which overwrites any existing value, if present; and assigning to the backing property of the indexer, for which the indexer is syntactic sugar (not applicable to C#, see F# or VB.NET examples).

The dictionary can also be initialized during construction using a "collection initializer", which compiles to repeated calls to Add.

Access by key Values are primarily retrieved using the indexer (which throws an exception if the key does not exist) and the TryGetValue() method, which has an output parameter for the sought value and a Boolean return-value indicating whether the key was found.

In this example, the sallyNumber value will now contain the string "555-9999".

Enumeration A dictionary can be viewed as a sequence of keys, sequence of values, or sequence of pairs of keys and values represented by instances of the KeyValuePair<TKey, TValue> type, although there is no guarantee of order. For a sorted dictionary, the programmer could choose to use a SortedDictionary<TKey, TValue> or use the .Sort LINQ extension method when enumerating. The following demonstrates enumeration using a foreach loop:

C++ In C++, there are four associative array classes: std::map (a red-black tree, equivalent to TreeMap in other languages) and std::unordered_map (a hash table, equivalent to HashMap in other languages). The other two are std::multimap and std::unordered_multimap, which permit multiple entries with the same key. These classes are templated which allow the data types of keys and values to be different for different collection instances. For a given instance of the map class the keys must be of the same base type. The same must be true for all of the values. Although std::map is typically implemented using a self-balancing binary search tree, C++11 defines a second map called std::unordered_map, which has the algorithmic characteristics of a hash table. This is a common vendor extension to the Standard Template Library (STL) as well, usually called hash_map, available from such implementations as SGI and STLPort.

Or less efficiently, as this creates temporary std::string values:

With the extension of initialization lists in C++11, entries can be added during a map's construction as shown below:

You can iterate through the list with the following code (C++03):

The same task in C++11:

Using the structured binding available in C++17:

Cobra Initializing an empty dictionary and adding items in Cobra:

Alternatively, a dictionary can be initialized with all items during construction:

The dictionary can be enumerated by a for-loop, but there is no guaranteed order:

ColdFusion Markup Language A structure in ColdFusion Markup Language (CFML) is equivalent to an associative array:

D D offers direct support for associative arrays in the core language; such arrays are implemented as a hash table with open addressing. The equivalent example would be:

Keys and values can be any types, but all the keys in an associative array must be of the same type, and the same goes for dependent values. Looping through all properties and associated values, and printing them, can be coded as follows:

A property can be removed as follows:

Delphi Delphi supports several standard containers, including TDictionary<T>:

Pre-2009 Delphi versions do not support associative arrays directly. Such arrays can be simulated using the TStrings class:

Erlang Erlang offers many ways to represent mappings; three of the most common in the standard library are keylists, dictionaries, and maps.

Keylists Keylists are lists of tuples, where the first element of each tuple is a key, and the second is a value. Functions for operating on keylists are provided in the lists module.

Accessing an element of the keylist can be done with the lists:keyfind/3 function:

Dictionaries Dictionaries are implemented in the dict module of the standard library. A new dictionary is created using the dict:new/0 function and new key/value pairs are stored using the dict:store/3 function:

Such a serial initialization would be more idiomatically represented in Erlang with the appropriate function:

The dictionary can be accessed using the dict:find/2 function:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Comparison of programming languages (associative array)

Start with the simplest possible case. Write down what Comparison of programming languages (associative array) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Comparison of programming languages (associative array) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Comparison of programming languages (associative array) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Comparison of programming languages (associative array)

In research
Comparison of programming languages (associative array) appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Comparison of programming languages (associative array) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Comparison of programming languages (associative array) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Associative arrays, Programming language comparisons, so understanding it makes those chapters shorter.
In everyday life
Look for Comparison of programming languages (associative array) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Comparison of programming languages (associative array)” →

How to study Comparison of programming languages (associative array) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Comparison of programming languages (associative array) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Comparison of programming languages (associative array) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Comparison of programming languages (associative array) in simple terms?

This comparison of programming languages (associative arrays) compares the features of associative array data structures or array-lookup processing for over 40 computer programming languages. Language support The following is a comparison of associative arrays (also "mapping", "hash", and "dictiona…

Why does Comparison of programming languages (associative array) matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Comparison of programming languages (associative array)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Comparison of programming languages (associative array).

Tags

  • Associative arrays
  • Programming language comparisons

Keep exploring