Autovivification means implicitly creating data structures accessed via name when explicitly creating their data, such as initializing a hash upon assigning the first key/value pair, or creating a folder upon saving a file in a new path.
Questions tagged [autovivification]
75 questions
228
votes
22 answers
What is the best way to implement nested dictionaries?
I have a data structure which essentially amounts to a nested dictionary. Let's say it looks like this:
{'new jersey': {'mercer county': {'plumbers': 3,
'programmers': 81},
'middlesex county':…

YGA
- 9,546
- 15
- 47
- 50
91
votes
4 answers
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'] =…

mike
- 46,876
- 44
- 102
- 112
30
votes
5 answers
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…

David B
- 29,258
- 50
- 133
- 186
26
votes
3 answers
What's the Ruby equivalent of Python's defaultdict?
In Python, I can make a hash where each element has a default value when it's first referenced (also know as "autovivification"). Here's an example:
from collections import defaultdict
d = defaultdict(int)
d["new_key"] += 1
print d
Printing the…

Ollie Glass
- 19,455
- 21
- 76
- 107
21
votes
5 answers
How to assign hash['a']['b']= 'c' if hash['a'] doesn't exist?
Is there any way simpler than
if hash.key?('a')
hash['a']['b'] = 'c'
else
hash['a'] = {}
hash['a']['b'] = 'c'
end

Radek
- 13,813
- 52
- 161
- 255
18
votes
6 answers
How to change behavior of dict() for an instance
So I'm writing a class that extends a dictionary which right now uses a method "dictify" to transform itself into a dict. What I would like to do instead though is change it so that calling dict() on the object results in the same behavior, but I…

Ceasar
- 22,185
- 15
- 64
- 83
18
votes
3 answers
Does PHP have autovivification?
Searching PHP.net for autovivification gives no results. At the time of writing, Wikipedia claims that only Perl has it. There are no clearly definitive results when searching Google for "php autovivification".
This PHP code runs…

Ollie Glass
- 19,455
- 21
- 76
- 107
16
votes
3 answers
Perl vivification question while dereferencing undefined array reference
I'm having tough time in understanding why the following works:
my $array_reference;
foreach $element (@{$array_reference}) {
# some code
}
while the following does not work
my $array_reference;
if (scalar (@{$array_reference}) {
# some code…

kuriouscoder
- 5,394
- 7
- 26
- 40
13
votes
4 answers
In a 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 =…

jbb
- 155
- 1
- 5
13
votes
2 answers
In python, how does the following AutoVivification class work?
In searching for a way of working with nested dictionaries, I found the following code posted by nosklo, which I would like to have explained, please.
class AutoVivification(dict):
"""Implementation of perl's autovivification feature."""
def…

Khono
- 133
- 6
11
votes
1 answer
Unexpected autovivification of arguments
Apparently my understanding of the no autovivification pragma is imperfect, as the not-dying-on-line-19 behaviour of the following script is extremely surprising to me.
use 5.014;
use strict;
use warnings;
no autovivification qw(fetch exists delete…

ryanm
- 2,979
- 18
- 22
11
votes
5 answers
How do I do advanced Python hash autovivification?
This question is about implementing the full Perl autovivification in Python. I know similar questions were asked before and so far the best answer is in "What is the best way to implement nested dictionaries in Python?" . However, I'm looking to do…

Zhang18
- 4,800
- 10
- 50
- 67
10
votes
1 answer
Hash keys behavior
perl -Mstrict -wlE 'my %h; say grep 0, $h{poluted}; say keys %h'
output
poluted
and
perl -Mstrict -wlE 'my %h; say grep 0, my @r= $h{poluted}; say keys %h'
gives no output.
I would like to know why outputs are different?

mpapec
- 50,217
- 8
- 67
- 127
9
votes
3 answers
PHP autovivification
Update: My original intention for this question was to determine if PHP actually has this feature. This has been lost in the answers' focus on the scalar issue. Please see this new question instead: "Does PHP have autovivification?" This question is…

Ollie Glass
- 19,455
- 21
- 76
- 107
8
votes
5 answers
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…

Беров
- 1,383
- 10
- 22