A feature I find useful in Perl is its multidimensional hash support. It's a great way for reconciling reports from different sources. Here is a Perl idiom I use often: -
my %hash; $hash{A}{A} = 1; $hash{A}{B} = 2; $hash{B}{A} = 4; $hash{B}{B} = 8; foreach my $k (keys %{$hash{B}}) { print "$k - $hash{B}{$k}\n"; }
Here is how one might achive the same thing in C++. The syntax will improve with the advent of Lambda expressions in C++09 spec. These remind me of Perl's map and stack ($_) idioms.
#include <map> #include <string> #include <iostream> using namespace std; void main() { map<string, map<string, int>> hash; hash["A"]["A"] = 1; hash["A"]["B"] = 2; hash["B"]["A"] = 4; hash["B"]["B"] = 8; for(map<string, int>::iterator i = hash["B"].begin(); i != hash["B +"].end(); i++) { cout << i->first << " - " << i->second << "\n"; } }
I was a little surprised to find C# does not make it very easy to use multidimensional hash tables. A few more seasoned C# programmers suggested using Generics. What is particularly unfriendly is the need to allocate and cast nested Hashtables. Perhaps, this is something Microsoft might fix in .NET version 4?
using System; using System.Collections.Generic; using System.Collections; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Hashtable hash = new Hashtable(); hash.Add("A", new Hashtable()); ((Hashtable)hash["A"]).Add("A", 1); ((Hashtable)hash["A"]).Add("B", 2); hash.Add("B", new Hashtable()); ((Hashtable)hash["B"]).Add("A", 4); ((Hashtable)hash["B"]).Add("B", 8); foreach (string k in ((Hashtable) hash["B"]).Keys) { Console.WriteLine(k + " - " + ((Hashtable) hash["B"])[ +k]); } } } }
Interestingly, Perl 6 only improves on version 5 slightly: -
my %hash; %hash{"A"}{"A"} = 1; %hash{"A"}{"B"} = 2; %hash{"B"}{"A"} = 4; %hash{"B"}{"B"} = 8; for %hash{"B"}.kv -> $key, $value { say "$key - $value" }
Update: There is an alternative way in C# using the Dictionary template. This does away with the need to cast.
using System; using System.Collections.Generic; using System.Collections; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Dictionary<string, Dictionary<string, int>> hash2 = new Di +ctionary<string,Dictionary<string,int>>(); hash2.Add("A", new Dictionary<string, int>()); hash2["A"].Add("A", 1); hash2["A"].Add("B", 2); hash2.Add("B", new Dictionary<string, int>()); hash2["B"].Add("A", 4); hash2["B"].Add("B", 8); foreach (string k in hash2["B"].Keys) { Console.WriteLine(k + " - " + hash2["B"][k]); } } } }

Replies are listed 'Best First'.
Re: Multidimesional hashs in Perl, C++, C# and Perl6
by duff (Parson) on May 04, 2007 at 13:59 UTC

    I'd write the perl 6 version slightly differently:

    my %hash; %hash<A><A> = 1; %hash<A><B> = 2; %hash<B><A> = 4; %hash<B><B> = 8; for %hash<B>.kv -> $key, $value { say "$key - $value" }

    It's a minor thing but using angle-brackets-as-quote-word for hash subscripts will be quite common I think once perl6 hits the streets.

Re: Multidimesional hashs in Perl, C++, C# and Perl6
by TedYoung (Deacon) on May 04, 2007 at 13:51 UTC

    I'll add some java to the mix:

    package Foo; import java.util.HashMap; public class Bar { public static void main(String... args) { HashMap<String, HashMap<String, Integer>> map = new HashMap<Stri +ng, HashMap<String, Integer>>(); map.put("A", new HashMap<String, Integer>()); map.get("A").put("A", 1); map.get("A").put("B", 2); map.put("B", new HashMap<String, Integer>()); map.get("B").put("A", 4); map.get("B").put("B", 8); for(String k: map.get("B").keySet()) { System.out.println(k + " - " + map.get("B").get(k)); } } }

    Java would be the language I use most after Perl. When using Java, I often think about how much I miss the first class support for lists and hashes that we have in Perl. But, I find that when I don't have that first class support, I write things differently anyhow. If it is harder to make and use a hash, I won't use a hash as often.

    That being said, I really love Java's hierarchy of collections. There are many powerful types of lists and hashes, and Generics really simplify things.

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
Re: Multidimesional hashs in Perl, C++, C# and Perl6
by parv (Parson) on May 04, 2007 at 20:32 UTC

    If you had used while( each ), you would have direct access to values (but I suppose that you already know that), similar to your Perl 6 syntax. For completeness sake ...

    my %hash = ( 'B' => { 'A' => 4 , 'B' => 8 } ); while ( my ( $k , $v ) = each %{ $hash{'B'} } ) { print "$k : $v\n"; }
      Shortened using the new placeholder variables:
      my %hash = ( A => { A => 1, B => 2 }, B => { A => 4, B => 8 }, ); say "$^k - $^v" for %hash<B>.kv;