Questions tagged [associative-array]

An associative array is an abstract data type composed of a collection of unique keys mapped to a collection of values.

An associative array (also associative container, map, mapping, dictionary, finite map, and in query-processing an index or index file) is an abstract data type composed of a collection of unique keys and a collection of values, where each key is associated with one value (or set of values). The operation of finding the value associated with a key is called a lookup or indexing, and this is the most important operation supported by an associative array. The relationship between a key and its value is sometimes called a mapping or binding. For example, if the value associated with the key "bob" is 7, we say that our array maps "bob" to 7. Associative arrays are very closely related to the mathematical concept of a function with a finite domain. As a consequence, a common and important use of associative arrays is in memoization.

See also

2924 questions
687
votes
8 answers

How to check if a specific key is present in a hash or not?

I want to check whether the "user" key is present or not in the session hash. How can I do this? Note that I don't want to check whether the key's value is nil or not. I just want to check whether the "user" key is present.
Mohit Jain
  • 40,277
  • 53
  • 161
  • 272
651
votes
17 answers

How do I remove objects from a JavaScript associative array?

Suppose I have this code: var myArray = new Object(); myArray["firstname"] = "Bob"; myArray["lastname"] = "Smith"; myArray["age"] = 25; Now if I wanted to remove "lastname"?....is there some equivalent of myArray["lastname"].remove()? (I need the…
Andrew
640
votes
15 answers

How to define hash tables in Bash?

What is the equivalent of Python dictionaries but in Bash (should work across OS X and Linux).
Sridhar Ratnakumar
  • 68,948
  • 61
  • 139
  • 172
518
votes
13 answers

Rename a dictionary key

Is there a way to rename a dictionary key, without reassigning its value to a new name and removing the old name key; and without iterating through dict key/value? In case of OrderedDict, do the same, while keeping that key's position.
rabin utam
  • 10,608
  • 3
  • 18
  • 15
383
votes
5 answers

How to iterate over associative arrays in Bash

Based on an associative array in a Bash script, I need to iterate over it to get the key and value. #!/bin/bash declare -A array array[foo]=bar array[bar]=foo I actually don't understand how to get the key while using a for-in loop.
pex
  • 6,731
  • 4
  • 30
  • 41
375
votes
23 answers

In PHP, how do you change the key of an array element?

I have an associative array in the form key => value where key is a numerical value, however it is not a sequential numerical value. The key is actually an ID number and the value is a count. This is fine for most instances, however I want a…
Thomas Owens
  • 107,741
  • 94
  • 299
  • 427
371
votes
11 answers

Extract subset of key-value pairs from Python dictionary object?

I have a big dictionary object that has several key value pairs (about 16), but I am only interested in 3 of them. What is the best way (shortest/efficient/most elegant) to achieve that? The best I know is: bigdict = {'a':1,'b':2,....,'z':26}…
Jayesh
  • 46,729
  • 19
  • 69
  • 96
223
votes
15 answers

Java associative-array

How can I create and fetch associative arrays in Java like I can in PHP? For example: $arr[0]['name'] = 'demo'; $arr[0]['fname'] = 'fdemo'; $arr[1]['name'] = 'test'; $arr[1]['fname'] = 'fname';
dobs
  • 2,643
  • 2
  • 19
  • 19
205
votes
9 answers

Dynamically creating keys in a JavaScript associative array

All the documentation I've found so far is to update keys that are already created: arr['key'] = val; I have a string like this: " name = oscar " And I want to end up with something like this: { name: 'whatever' } That is, split the string and…
OscarRyz
  • 184,433
  • 106
  • 369
  • 548
186
votes
12 answers

How to loop through an associative array and get the key?

My associative array: $arr = array( 1 => "Value1", 2 => "Value2", 10 => "Value10" ); Using the following code, $v is filled with $arr's values foreach($arr as $v){ echo($v); // Value1, Value2, Value10 } How do I get $arr's keys…
Robin Rodricks
  • 99,791
  • 133
  • 372
  • 575
166
votes
4 answers

Union of dict objects in Python

How do you calculate the union of two dict objects in Python, where a (key, value) pair is present in the result iff key is in either dict (unless there are duplicates)? For example, the union of {'a' : 0, 'b' : 1} and {'c' : 2} is {'a' : 0, 'b' :…
Mechanical snail
  • 26,499
  • 14
  • 83
  • 107
128
votes
14 answers

How to concatenate properties from multiple JavaScript objects

I am looking for the best way to "add" multiple JavaScript objects (associative arrays). For example, given: a = { "one" : 1, "two" : 2 }; b = { "three" : 3 }; c = { "four" : 4, "five" : 5 }; what is the best way to compute: { "one" : 1, "two" : 2,…
Vlad
  • 8,407
  • 4
  • 39
  • 60
125
votes
17 answers

Associative arrays in Shell scripts

We required a script that simulates Associative arrays or Map like data structure for Shell Scripting, any body?
Irfan Zulfiqar
  • 1,739
  • 4
  • 14
  • 18
118
votes
11 answers

Fastest way to implode an associative array with keys

I'm looking for a fast way to turn an associative array in to a string. Typical structure would be like a URL query string but with customizable separators so I can use '&' for xhtml links or '&' otherwise. My first inclination is to use foreach…
matpie
  • 15,700
  • 9
  • 58
  • 80
117
votes
5 answers

PHP prepend associative array with literal keys?

Is it possible to prepend an associative array with literal key=>value pairs? I know that array_unshift() works with numerical keys, but I'm hoping for something that will work with literal keys. As an example I'd like to do the following: $array1 =…
Colin Brock
  • 20,219
  • 9
  • 43
  • 61
1
2 3
99 100