0

i have a String that contains a Hash Reference, for example:

my $string = "HASH(0x5602d43a7648)";

How can i access the hash from this reference and print out it's values and keys?

1 Answers1

3

You can't, it's too late.

If the variable contains a reference, you can dereference it, though:

my $hash_ref = { a => 42 };
my %hash = %{ $hash_ref };

but once you stringify it, there's no way:

my $string = "$hash_ref";

To create a hash within a hash, or a "Hash of Hashes (HoH)", use the references, not strings:

my %hash = ( key1 => { subkey1 => 'val1',
                       subkey2 => 'val2' },
             key2 => { subkey3 => 'val3' } );
print $hash{key1}{subkey2}, "\n";  # val2
choroba
  • 231,213
  • 25
  • 204
  • 289
  • hey thank you very much for your answer. The variable contains a reference within a longer string, and the variable itself is the value of a key in a hash. So its kinda a hash within a hash, however when i remove all other chars from the string and try to dereference it like you did: my %hash = %{ $string }; (where the string only contains "HASH(0x5602d43a7648)") i get an error. i simply want the hash within the hash. – NeedPerlHalp Jun 28 '18 at 13:06
  • See https://stackoverflow.com/q/1671281 – daxim Jun 28 '18 at 14:45