by fgasper · 6y ago · 0 comments
Umm … ok:
my %dict = ( foo => [ 1, 2, 3 ], bar => [ 4, 5, 6 ], );
> Now write a dictionary of 2d arrays.
my %dict2d = ( foo => [ [1, 2, 3], [4, 5, 6] ], bar => [], );
It’s pretty much identical to JS. Not sure how that’s supposed to exemplify how hard it is to write Perl.
To be clear, even a nested hash is all well as long as the depth and the keys are literals (as in your example). It is when keys are dynamic that things get unruly. And god forbid if the _depth_ of nesting is dynamic. All of these are trivial to implement in Python, Ruby, JS, PHP, ...
%nested = (foo => { bar => { baz => "quux" } });
Access the leaf with $nested->{foo}{bar}{baz}
If the keys are in variables, it becomes $nested->{$x}{$y}{$z}
For dynamic depth, do you have a specific use case in mind? If I have a tree, I’m probably going to search it rather than using a hardcoded path. $nested{$x}{$y}{$z}
or after initializing with curly brackets rather than parentheses to construct a new reference to an anonymous hash $nested = { … };
and then use the arrow to dereference as in the grandparent.