4 ms·
I totally understand your question, as I was struggling to do this kind of things in Perl 5 some time ago. I hear that it's been made easier in Perl 5 lately,
by grondilu 11y ago
I totally understand your question, as I was struggling to do this kind of things in Perl 5 some time ago. I hear that it's been made easier in Perl 5 lately, though. In any case, it's indeed much easier in Perl 6, as in Perl 6, there is no more references, or rather, everything is a reference.
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!
- justinator 11y agoLittle syntax error: for hashes, you would say: 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.
- grondilu 11y agoOh I was sure the syntax was ok in both forms. I'm guessing it's a GLR revision. It's indeed better to to reserve {} for scalar containers. 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" }⏏;
- justinator 11y agoThat's good, because in Perl 5, it does DWIM, but that sometimes means returning undef, which - erm, thanks Perl ;)