3 ms·
That's interesting, because defining an AoH or HoA in Perl is almost identical to how you would do it in Javascript, which is to say it looks a lot like JSON.
by kbenson 12y ago
That's interesting, because defining an AoH or HoA in Perl is almost identical to how you would do it in Javascript, which is to say it looks a lot like JSON.
If it was the access and assignment semantics, the normal case is also fairly straightforward; use curly braces for hash/dict keys, square brackets for array indices.
E.g.
my %data = (
foo => [
{ bar => 10, baz => 12 },
{ bar => 20, baz => 13 },
{ bar => 30, baz => 14 },
],
);
print $data{foo}[2]{bar}; # 30
push $data{foo}, { bar => 40, baz => 15 };
Given, using array and hash references with the built-in push,pop,shift,unshift,keys,values and each used to require some annoying dereferencing that could be confusing.
Do you still find that confusing?
- ghshephard 12y agoI'll admit to not having looked at it for about 12 years, and, of course, I'm not saying I couldn't figure out after 2-3 minutes over looking over the pattern, but it just never flowed the way it did with Python for me. data ={} data['foo']= [ {'bar':10,'baz':12}, {'bar':20,'baz':13}, {'bar':30,'baz':14} ] print data['foo'][2]['bar'] data['foo'].append({'bar':40,'baz':15}) for x in data['foo']: print x['bar'] Maybe it had something to do with indices always being [] brackets and {} only being used to define the dict structure. Or maybe it got easier sometime in the last 12 years? Or, maybe you could onto something, and having come pre-primed with knowledge hashes/arrays, I just picked up python more quickly. Anyways, it was just a personal story told as well as I could remember it.