(no title)
Ultimatt | 5 months ago
my $a = ["foo", 123, {"bar" => [[1,2,3], {"qux" => "quux"}]}];
$a->[2]{"bar"}[1]{"spam"} = "eggs";
use JSON;
print(encode_json($a))
That's the same as your example (minus the tuple type), but where Perl shines over Python (a lot) is you could have done the following: use JSON;
my $a = [];
$a->[2]{"bar"}[1]{"spam"} = "eggs";
print(encode_json($a))
which would yield the json: [null,null,{"bar":[null,{"spam":"eggs"}]}]To do this in Python is truly grim:
import json
from collections import defaultdict
# recursive defaultdict using lambda
datastructure = lambda: defaultdict(datastructure)
a = [None] * 3
a[2] = datastructure()
a[2]["bar"][1]["spam"] = "eggs"
print(json.dumps(a))
and thats doing it the unpythonic way, if you were to do this like the typical Python dev would accept in an MR for a large corp you would have written: import json
from collections import defaultdict
def datastructure():
return defaultdict(datastructure)
a = []
# expand list to at least length 3
while len(a) < 3:
a.append(None)
a[2] = datastructure()
a[2]["bar"][1]["spam"] = "eggs"
print(json.dumps(a))
They would still hate you for defaultdict(datastructure) though. Because absolutely no one in Python realises its got that level of expressionism one of the few places it does.
kstrauser|5 months ago
Enk1du|4 months ago
odc|4 months ago
So when you see
then $a is an array reference where each item is a hashmap. But with then $a is an array reference where each item is a reference to a hashmap.