Jump to content

Associative array/Creation: Difference between revisions

(added swift)
Line 1,419:
 
=={{header|Nimrod}}==
<lang nimrod>import tables
import tables
 
var
var t: TTable[int,string] = initTable[int,string]()
hash = initTable[string, int]() # empty hash table
hash2 = {"key1": 1, "key2": 2}.toTable # hash table with two keys
hash3 = [("key1", 1), ("key2", 2)].toTable # hash table from tuple array
hash4 = @[("key1", 1), ("key2", 2)].toTable # hash table from tuple seq
value = hash2["key1"]
 
thash[1"spam"] = "one"1
thash[2"eggs"] = "two"2
hash.add("foo", 3)
t[3] = "three"
t.add(4,"four")
 
echo "thash has ", & $thash.len &, " elements"
echo "hash has t key 5foo? ", & $thash.hasKey(5"foo")
echo "hash has t key 4bar? ", & $thash.hasKey(4"bar")
 
echo "iterate pairs:" # iterating over (key, value) pairs
echo "has t key 4? " & $t.hasKey(4)
for key, value in hash:
echo "has t key 5? " & $t.hasKey(5)
echo key, ": ", value
 
#echo "iterate keys:" # iterating over keys
echofor "key iterationin hash.keys:"
echo key
for k in t.keys:
echo "at[" & $k & "]=" & t[k]
 
echo "iterate values:" # iterating over values
#itetate pairs
for kkey in thash.keysvalues:
echo "pair iteration:"
echo key</lang>
for k,v in t.pairs:
echo "at[" & $k & "]=" & v
</lang>
Output:<br/>
<pre>hash has 3 elements
thash has 4key elementsfoo? true
hashash thas key 4bar? truefalse
iterate pairs:
has t key 5? false
eggs: 2
key iteration:
foo: 3
at[1]=one
spam: 1
at[2]=two
iterate keys:
at[3]=three
eggs
at[4]=four
foo
pair iteration:
spam
at[1]=one
iterate values:
at[2]=two
2
at[3]=three
3
at[4]=four
1</pre>
 
=={{header|Objeck}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.