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]); } } } }

In reply to Multidimesional hashs in Perl, C++, C# and Perl6 by bsdz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.