autovivification

What's the best way to initialize a dict of dicts in Python?

A lot of times in Perl, I'll do something like this: $myhash{foo}{bar}{baz} = 1 How would I translate this to Python? So far I have: if not 'foo' in myhash: myhash['foo'] = {} if not 'bar' in myhash['foo']: myhash['foo']['bar'] = {} myhash['foo']['bar']['baz'] = 1 Is there a better way? ...

How do I disable autovivification in Perl?

Suppose you have a HUGE application "develoopped" ;) by a big team. Here is a simplified model of the potential disaster that may occur when somebody checks too deep in a data structure. If not possible to disable autovification completely or in scope, how to work around this? Thank you very much :) !!!! use strict; use warnings;use Dat...

Ruby Autovivification

I've been trying to use autovivification in ruby to do simple record consolidation on this: 2009-08-21|09:30:01|A1|EGLE|Eagle Bulk Shpg|BUY|6000|5.03 2009-08-21|09:30:35|A2|JOYG|Joy Global Inc|BUY|4000|39.76 2009-08-21|09:30:35|A2|LEAP|Leap Wireless|BUY|2100|16.36 2009-08-21|09:30:36|A1|AINV|Apollo Inv Cp|BUY|2300|9.15 2009-08-21|09:30:...

Why does Perl's autovivification work in this case?

Can some one help me understand the output of this Perl program: use Data::Dumper; my %hash; $hash{hello} = "foo"; $hash{hello}{world} = "bar"; print $hash{hello} . "\n"; print $hash{hello}{world} . "\n"; print Dumper(\%hash); And the output: foo bar $VAR1 = { 'hello' => 'foo' }; Where is the "foo" coming from? H...

Why does Perl autovivify in this case?

Why does $a become an arrayref? I'm not pushing anything to it. perl -MData::Dumper -e 'use strict; 1 for @$a; print Dumper $a' $VAR1 = []; ...

advanced python autovivification

This question is about implementing the full Perl autovivification in python. I know similar questions were asked before and so far the best answre is http://stackoverflow.com/questions/635483/what-is-the-best-way-to-implement-nested-dictionaries-in-python/652284#652284. However, I'm looking to do this: a['x']['y'].append('z') without...

In a Python dict of dicts, how do you emulate Perl's auto-vivification behavior?

Both Google and the online docs are not delivering much insight on my query, so I thought I would ask the community here. In Perl, you can easily setup a hash-of-a-hash-of-a-hash and test the final key like so: my $hash = {}; $hash{"element1"}{"sub1"}{"subsub1"} = "value1"; if (exists($hash{"element1"}{"sub1"}{"subsub1"})) { print "...

Multiple initialization of auto-vivifying hashes using a new operator in Ruby

I would like to initialize several auto-vivifying hashes by one-line expression. So far I came to an extra method for the AutoHash object: class AutoHash < Hash ... def few(n=0) Array.new(n) { AutoHash.new } end which allows me to do the following a, b, c = AutoHash.new.few 3 However, I feel that one can make the followin...

How to handle combination []+= for auto-vivifying hash in Ruby?

In order to implement auto-vivification of Ruby hash, one can employ the following class class AutoHash < Hash def initialize(*args) super() @update, @update_index = args[0][:update], args[0][:update_key] unless args.empty? end def [](k) if self.has_key?k super(k) else AutoHash.new(:update => self, :u...

How can I check if a key exists in a deep Perl hash?

If I understand correctly, calling if (exists $ref->{A}->{B}->{$key}) { ... } will spring into existence $ref->{A} and $ref->{A}->{B} even if they did not exist prior to the if! This seems highly unwanted. So how should I check if a "deep" hash key exists? ...

Why is `exists` modifying my constant?

The exists function can unexpectedly autovivify entries in hashes. What surprises me is that this behavior carries over to constants as well: use strict; use warnings; use Data::Dump 'dump'; use constant data => { 'foo' => { 'bar' => 'baz', }, ...