Difference between revisions of "Traverse a Map"

From CodeCodex

Line 4: Line 4:
  
 
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.
 
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.
<HIGHLIGHTSYNTAX language="cpp">
+
<pre class="c++">
 
std::cout << "The map contains the following associations:" << std::endl;
 
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
 
for (std::map<K,V>::iterator it = theMap.begin(); i != theMap.end(); i++) { // iterate over the entries
Line 11: Line 11:
 
     std::cout << "(" << key << "," << value << ")" << std::endl;
 
     std::cout << "(" << key << "," << value << ")" << std::endl;
 
}
 
}
</HIGHLIGHTSYNTAX>
+
</pre>
  
 
===D===
 
===D===
Line 24: Line 24:
  
 
===Go===
 
===Go===
<HIGHLIGHTSYNTAX>
+
<pre>
 
fmt.Println("The map contains the following associations:")
 
fmt.Println("The map contains the following associations:")
 
for key, value := range theMap {
 
for key, value := range theMap {
 
     fmt.Printf("(%v,%v)\n", key, value)
 
     fmt.Printf("(%v,%v)\n", key, value)
 
}
 
}
</HIGHLIGHTSYNTAX>
+
</pre>
  
 
===Java===
 
===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.
 
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.
<HIGHLIGHTSYNTAX language="java122">
+
<pre class="java">
 
System.out.println("The map contains the following associations:");
 
System.out.println("The map contains the following associations:");
 
for (Map.Entry<K,V> e : theMap.entrySet()) { // iterate over the Set of entries
 
for (Map.Entry<K,V> e : theMap.entrySet()) { // iterate over the Set of entries
Line 40: Line 40:
 
     System.out.println("(" + key + "," + value + ")");
 
     System.out.println("(" + key + "," + value + ")");
 
}
 
}
</HIGHLIGHTSYNTAX>
+
</pre>
  
 
===OCaml===
 
===OCaml===
 
If you want to just iterate through a map, you could use the iter function:
 
If you want to just iterate through a map, you could use the iter function:
<HIGHLIGHTSYNTAX language="ocaml">
+
<pre class="ocaml">
 
module MyMap = Map.Make (String);;
 
module MyMap = Map.Make (String);;
 
let the_map = MyMap.add "foo" 1 MyMap.empty;;
 
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" *)
 
MyMap.iter (fun k v -> Printf.printf "%s -> %d\n" k v) the_map;; (* outputs "foo -> 1" *)
</HIGHLIGHTSYNTAX>
+
</pre>
  
 
Or you can use a fold to collect the results together to do something:
 
Or you can use a fold to collect the results together to do something:
<HIGHLIGHTSYNTAX language="ocaml">
+
<pre class="ocaml">
 
let sum_of_values = MyMap.fold (fun k v z -> v + z) the_map 0;;
 
let sum_of_values = MyMap.fold (fun k v z -> v + z) the_map 0;;
</HIGHLIGHTSYNTAX>
+
</pre>
  
 
If you wanted to traverse a hashtable:
 
If you wanted to traverse a hashtable:
<HIGHLIGHTSYNTAX language="ocaml">
+
<pre class="ocaml">
 
let the_hash = Hashtbl.create 10;;
 
let the_hash = Hashtbl.create 10;;
  
 
Hashtbl.iter (fun k v -> Printf.printf "%s -> %d\n" k v) the_hash;;
 
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;;
 
let sum_of_values = Hashtbl.fold (fun k v z -> v + z) the_hash 0;;
</HIGHLIGHTSYNTAX>
+
</pre>
  
 
===Perl===
 
===Perl===
<HIGHLIGHTSYNTAX language="perl">
+
<pre class="perl">
 
print "The map contains the following associations:\n";
 
print "The map contains the following associations:\n";
 
while ($key, $val = each %theMap) {
 
while ($key, $val = each %theMap) {
 
     print "($key,$val)\n";
 
     print "($key,$val)\n";
 
}
 
}
</HIGHLIGHTSYNTAX>
+
</pre>
  
 
Alternately, you can get the keys or values with the "keys()" and "values()" functions, and then iterate them:
 
Alternately, you can get the keys or values with the "keys()" and "values()" functions, and then iterate them:
<HIGHLIGHTSYNTAX language="perl">
+
<pre class="perl">
 
print "The map contains the following associations:\n";
 
print "The map contains the following associations:\n";
 
foreach $key (keys %theMap) {
 
foreach $key (keys %theMap) {
 
     print "($key,$theMap{$key})\n";
 
     print "($key,$theMap{$key})\n";
 
}
 
}
</HIGHLIGHTSYNTAX>
+
</pre>
  
 
===PHP===
 
===PHP===
<HIGHLIGHTSYNTAX language="php3">
+
<pre class="php">
 
print("The map contains the following associations:\n");
 
print("The map contains the following associations:\n");
 
foreach ($array as $key => $val) {
 
foreach ($array as $key => $val) {
 
     printf("(%s,%s)\n", $key, $val);
 
     printf("(%s,%s)\n", $key, $val);
 
}
 
}
</HIGHLIGHTSYNTAX>
+
</pre>
  
 
===Python===
 
===Python===
<HIGHLIGHTSYNTAX language="python">
+
<pre class="python">
 
     print "The map contains the following associations:"
 
     print "The map contains the following associations:"
 
     for key, val in theMap.iteritems():
 
     for key, val in theMap.iteritems():
 
         print "(%s,%s)" % (key, val)
 
         print "(%s,%s)" % (key, val)
</HIGHLIGHTSYNTAX>
+
</pre>
  
 
===Ruby===
 
===Ruby===
 
This code assumes that theMap is an object of the Hash class.  
 
This code assumes that theMap is an object of the Hash class.  
<pre>
+
<pre class="ruby">
 
print "The map contains the following associations:\n"
 
print "The map contains the following associations:\n"
  

Revision as of 04:41, 2 March 2011

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;