so for instance a dictionary of dictionaries or arrays looks like:
my %dict = {
'foo' => {
'bar' => [ 'an', 'array' ],
'a deeply nested word' => "tada!"
},
'an empty dict' => {},
'an empty array' => []
};
say %dict{'foo'}{'a deeply nested word'}; # tada! my %hash = (
# your key/values
);
and not, my %hash = {
# your key/values
};
I think that may be one of the problems people have with Perl - the syntax gets a little whacky and it's hard, sometimes, to wrap your mind around Perl-style references.My personal way to solve this is to almost exclusively use references in complex data structures, so:
my $hashref = {
another_hashref => {
key_name => [qw(
value
is
an
array_ref
)],
},
};
Is a hashref, that contains another hashref, which contains an array ref. Easy to bring up the 3rd thingy in the arrayref: print $hashref->{another_hashref}->{key_name}->[2]; #an
This should be somewhat palatable to do actually, if you're slinging JSON all day.Notice how in Perl 6 you get a nice error message:
$ perl6 -e 'my %h = { foo => "bar" };'
Potential difficulties:
Useless use of hash composer on right side of hash assignment;
did you mean := instead?
at -e:1
------> my %h = { foo => "bar" }⏏;Are you talking about something like this:
$hash_of_hash = {
hash1 => {key1 => 'value1'},
hash2 => {key1 => 'value1'}
};
say $hash_of_hash->{hash1}{key1};
Seems easy to me...I think reference syntax in Perl6 is gone. Which personally I don't like.
Both are cleared up by really understanding context as it applies in Perl 5, which is probably the single largest difference to other languages that is (sometimes unfortunately) easy to get along without understanding because of the DWIMiness of the language. Or references, but I tend to think most people should be able to understand them without too much trouble.
%hash = {key1 => 'value1'}
$hash_of_hash->{hash1} = %hash
Now, this will work (or something like it, I don't remember the syntax):
%hash = {key1 => 'value1'}
$hash_of_hash->{hash1} = \%hash
The syntax makes a lot of sense when you read the specs. It does create lots of problems when you try to create complex data structures in practice. It's also complex enough (with enough corner cases) that I can't remember it if I don't use it every week - but maybe that's just me.
By the way, I don't remember whether I must read the above structure as:
$hash_of_hash->{hash1}{key1}
or
$($hash_of_hash->{hash1}){key1}
I also don't remember if it is equal to the one you defined. I know it's in the documentation, and it's very clear there. It's just some corner case that I never bothered to memorize for any long time.
%hash = (hello => 'there');
%hash = ('hello', 'there');
This is also perfectly valid (though non-idiomatic) Perl 5: print Hello => ' ' => there => "\n";
Will I be able to write a dictionary of dictionaries or arrays without looking at the documentation every time? Did Perl 6 syntax for references got sane?