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

Content deleted Content added
Python: changing "pycon" to "python" in syntaxhighlight tag (using wikEd)
mNo edit summary
Line 1,336:
An associative array can be declared using the following syntax:
 
<syntaxhighlight lang=PHP"php">
$phonebook = array();
$phonebook['Sally Smart'] = '555-9999';
Line 1,367:
PHP can loop through an associative array as follows:
 
<syntaxhighlight lang=PHP"php">
foreach ($phonebook as $name => $number) {
echo 'Number for ', $name, ': ', $number, "\n";
}
 
// For the last array example it is used like this
foreach ($phonebook['contacts'] as $name => $num) {
echo 'Name: ', $name, ', number: ', $num['number'], "\n";
}
Line 1,385:
[[Pike (programming language)|Pike]] has built-in support for associative arrays, which are referred to as mappings. Mappings are created as follows:
 
<syntaxhighlight lang=Pike"pike">
mapping(string:string) phonebook = ([
"Sally Smart":"555-9999",
Line 1,397:
Iterating through a mapping can be done using <code>foreach</code>:
 
<syntaxhighlight lang="pike">
foreach(phonebook; string key; string value) {
write("%s:%s\n", key, value);
Line 1,405:
Or using an iterator object:
 
<syntaxhighlight lang="pike">
Mapping.Iterator i = get_iterator(phonebook);
while (i->index()) {
Line 1,415:
Elements of a mapping can be removed using <code>m_delete</code>, which returns the value of the removed index:
 
<syntaxhighlight lang="pike">
string sallys_number = m_delete(phonebook, "Sally Smart");
</syntaxhighlight>
Line 1,532:
 
<syntaxhighlight lang="python">
>>> del phonebook['"John Doe'"]
>>> val = phonebook.pop('"Sally Smart'")
>>> phonebook.keys() # Only one key left
['J. Random Hacker']
Line 1,566:
A map can be written as a literal, such as <code>#(key1 value1 key2 value2 ...)</code>, or can be created using <code>make map! [key1 value1 key2 value2 ...]</code>:
 
<syntaxhighlight lang=Red"red">
Red [Title:"My map"]
 
Line 1,609:
In [[REXX]], associative arrays are called "stem variables" or "Compound variables".
 
<syntaxhighlight lang=REXX"rexx">
KEY = 'Sally Smart'
PHONEBOOK.KEY = '555-9999'
Line 1,621:
by convention contains the total number of items in the stem:
 
<syntaxhighlight lang=REXX"rexx">
NAME.1 = 'Sally Smart'
NAME.2 = 'John Doe'
Line 1,634:
In [[Ruby programming language|Ruby]] a hash table is used as follows:
 
<syntaxhighlight lang="irb">
irb(main):001:0> phonebook = {
irb(main):002:1* 'Sally Smart' => '555-9999',
Line 1,647:
Ruby supports hash looping and iteration with the following syntax:
 
<syntaxhighlight lang="irb">
irb(main):007:0> ### iterate over keys and values
irb(main):008:0* phonebook.each {|key, value| puts key + " => " + value}
Line 1,726:
[[Scala (programming language)|Scala]] provides an immutable <code>Map</code> class as part of the <code>scala.collection</code> framework:
 
<syntaxhighlight lang=Scala"scala">
val phonebook = Map("Sally Smart" -> "555-9999",
"John Doe" -> "555-1212",
Line 1,734:
Scala's [[type inference]] will decide that this is a <code>Map[String, String]</code>. To access the array:
 
<syntaxhighlight lang=Scala"scala">
phonebook.get("Sally Smart")
</syntaxhighlight>
Line 1,743:
In [[Smalltalk]] a <code>Dictionary</code> is used:
 
<syntaxhighlight lang=Smalltalk"malltalk">
phonebook := Dictionary new.
phonebook at: 'Sally Smart' put: '555-9999'.
Line 1,752:
To access an entry the message <code>#at:</code> is sent to the dictionary object:
 
<syntaxhighlight lang=Smalltalk"smalltalk">
phonebook at: 'Sally Smart'
</syntaxhighlight>
Line 1,758:
Which gives:
 
<syntaxhighlight lang=Text"text">
'555-9999'
</syntaxhighlight>