Associative array/Creation: Difference between revisions
Content added Content deleted
m (→[[Ruby]]: Updated formatting. Added link to nil.) |
|||
Line 138: | Line 138: | ||
hash[777] # => 'unknown key 777' |
hash[777] # => 'unknown key 777' |
||
hash[666] # => 'devil' |
hash[666] # => 'devil' |
||
==[[OCaml]]== |
|||
A simple idiom to create a hash table mapping strings to integers: |
|||
let hash = Hashtbl.create 0;; |
|||
List.iter (fun (key, value) -> Hashtbl.add hash key value) |
|||
["foo", 5; "bar", 10; "baz", 15];; |
|||
To retrieve a value: |
|||
let bar = Hashtbl.find hash "bar";; (* bar = 5 *) |
|||
To retrieve a value, returning a default if the key is not found: |
|||
let quux = try Hashtbl.find hash "quux" with Not_found -> some_value;; |