Traverse a Map

From CodeCodex

Revision as of 04:41, 2 March 2011 by 211.2.129.92 (Talk)

Implementations

C++

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(); i != theMap.end(); i++) { // 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

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

F#

Map.fold

Go

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

Java

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

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

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

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

Python

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

Ruby

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

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;