MiSo
MiSo

Reputation: 463

How to work with hash of hashes in Perl?

I want to build a struct in Perl that has a hash and a variable,then create a hash that each field will contain the struct I created. like this:

use Class::Struct;

struct exmpl => {hash=>'%' , val => '$'}; 
my %hash_of_structs;
$hash_of_structs { "one" } = exmpl -> new ();

Now hash_of_structs has a field with "one" key that contains the struct exmpl. My question is how do I push new values into the hash that is inside the struct?

I figured how to work with the value in the struct:

$hash_of_structs { "one" } -> val ("1");
printf ( "The value is: %d\n",$hash_of_structs { "one" }-> val );

But it's not working the same way with the hash in the struct. I tried:

$hash_of_structs { "one" } => hash{"uno"}("1");

Thanks :)

Upvotes: 0

Views: 193

Answers (1)

choroba
choroba

Reputation: 242198

Use the following syntax. If a hash reference is passed, the old content is forgotten, if you supply two arguments, a key - value pair is added.

$hash_of_structs{one}->hash({'A', 'a', 'B', 'b'});
$hash_of_structs{one}->hash('key', 'value');

Upvotes: 1

Related Questions