in reply to Algorithmic hash bug.

If I read your question correctly, it is very simple:
$x = $hash{$valuem};
The solution you provided implies: a hash with the key 'mainKey', whose value is a reference to a hash with the key in $valueM, whose value is a reference to a hash with the key 'key'.