Traverse a Map

From CodeCodex

Implementations[edit]

C++[edit]

This code assumes that theMap is an object of type std::map<K,V>, that is, with key type K and value type V. It will loop over theMap and will list the name values pairs contained within the map.

std::cout << "The map contains the following associations:" << std::endl;
for (std::map<K,V>::iterator it = theMap.begin(); it != theMap.end(); it++) { // iterate over the entries
    K key = it->first;  // Get the key
    V value = it->second;  // Get the value
    std::cout << "(" << key << "," << value << ")" << std::endl;
}

D[edit]

writefln("The map contains the following associations:");
foreach(key, value; theMap)
    writefln("(%s,%s)", key, value);

Erlang[edit]

This code assumes that theMap is the process dictionary.

traverse() ->
    lists:foreach(fun({Key, Value}) -> io:format("(~p,~p)~n", [Key, Value]) end, get()).

F#[edit]

Map.fold

Go[edit]

fmt.Println("The map contains the following associations:")
for key, value := range theMap {
    fmt.Printf("(%v,%v)\n", key, value)
}

Java[edit]

This code assumes that theMap is already instantiated, and is a Map<K,V>, that is, with key type K and value type V. It will loop over theMap and will list the name values pairs contained within the map.

System.out.println("The map contains the following associations:");
for (Map.Entry<K,V> e : theMap.entrySet()) { // iterate over the Set of entries
    K key = e.getKey();  // Get the key
    V value = e.getValue();  // Get the value
    System.out.println("(" + key + "," + value + ")");
}

OCaml[edit]

If you want to just iterate through a map, you could use the iter function:

module MyMap = Map.Make (String);;
let the_map = MyMap.add "foo" 1 MyMap.empty;;
MyMap.iter (fun k v -> Printf.printf "%s -> %d\n" k v) the_map;; (* outputs "foo -> 1" *)

Or you can use a fold to collect the results together to do something:

let sum_of_values = MyMap.fold (fun k v z -> v + z) the_map 0;;

If you wanted to traverse a hashtable:

let the_hash = Hashtbl.create 10;;

Hashtbl.iter (fun k v -> Printf.printf "%s -> %d\n" k v) the_hash;;
let sum_of_values = Hashtbl.fold (fun k v z -> v + z) the_hash 0;;

Perl[edit]

print "The map contains the following associations:\n";
while ($key, $val = each %theMap) {
    print "($key,$val)\n";
}

Alternately, you can get the keys or values with the "keys()" and "values()" functions, and then iterate them:

print "The map contains the following associations:\n";
foreach $key (keys %theMap) {
    print "($key,$theMap{$key})\n";
}

PHP[edit]

print("The map contains the following associations:\n");
foreach ($array as $key => $val) {
    printf("(%s,%s)\n", $key, $val);
}

Python[edit]

     print "The map contains the following associations:"
     for key, val in theMap.iteritems():
         print "(%s,%s)" % (key, val)

Ruby[edit]

This code assumes that theMap is an object of the Hash class.

print "The map contains the following associations:\n"

for key, val in theMap
  printf "(%s,%s)\n", key, val
end

# or
theMap.each {|key, val| puts "(#{key},#{val})"}

Seed7[edit]

This code assumes that the hash table theMap is already instantiated. It will loop over theMap and will list the name value pairs contained within the hash table.

const proc: writeTheMap is func
  local
    var base_type(typeof(theMap)): valueVar is base_type(typeof(theMap)).value;
    var key_type(typeof(theMap)): keyVar is key_type(typeof(theMap)).value;
  begin
    writeln("The map contains the following associations:");
    for valueVar key keyVar range theMap do
      writeln("    (" <& keyVar <& "," <& valueVar <& ")");
    end for;
  end func;