Questions tagged [autovivification]

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.

75 questions
2
votes
3 answers

How to nonvivificate in Perl

Let's say I have a Perl script that does: my $hash = {}; $hash->{'a'} = {aa => 'b'}; $hash->{'b'} = undef; for (qw(a b c)) { if(defined $hash->{$_}->{aa}) { say "defined $_"; } else { say "undef $_"; …
Eric Fossum
  • 2,395
  • 4
  • 26
  • 50
1
vote
0 answers

problem loading Autovivification file when moving from python 2.7 to 3.6, KeyError: 'DictType'

In python 2.7, I have a bunch of files stored as the following object: class AutoVivification(dict): """Implementation of perl's autovivification feature.""" def __getitem__(self, item): try: return…
jason
  • 3,811
  • 18
  • 92
  • 147
1
vote
1 answer

Why autovivification works at 'for'?

I have next code: print for @{ $events->{ $name } }; When $events is empty hash I got $name key which value is empty array [] Why autovivification works despite on that I just access element and assign nothing to it?
Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
1
vote
2 answers

Is there autovivification for Go?

Is there autovivification for Go? As @JimB correctly noticed, my definition is not that strict. About my goal: In Python we have a very elegant "emulation" for an autovivification: class Path(dict): def __missing__(self, key): value…
Michael Dorner
  • 17,587
  • 13
  • 87
  • 117
1
vote
3 answers

Generic autovivify function for Maps

How can I create vivify a key with generics? This code does not even compile: /* populate the map with a new value if the key is not in the map */ private boolean autoVivify(Map map, K key) { if (! map.containsKey(key)) { …
perreal
  • 94,503
  • 21
  • 155
  • 181
1
vote
3 answers

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…
Andrei
  • 10,918
  • 12
  • 76
  • 110
1
vote
1 answer

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…
Andrei
  • 10,918
  • 12
  • 76
  • 110
1
vote
1 answer

Python One-Line Tree using defaultdict. How to reduce the number of arguments required?

I'm using this gist's defaultdict one-line tree. def tree(): return defaultdict(tree) Currently, you must provide a separate [] for every node you want to add. ie: users =…
JmsBtlr111
  • 31
  • 1
  • 5
1
vote
1 answer

Can't locate autovivification.pm in @INC

I get the following error when I run my Perl script: "Can't locate autovivification.pm in @INC" How can I install this module on my system?
Poisson
  • 1,543
  • 6
  • 23
  • 34
1
vote
2 answers

Perl defining undefined keys

The following code prints Key defined 3. Why is Perl defining the key ABC ? I was expecting all the three checks to be false.What am I doing incorrectly ? #!/usr/bin/perl use warnings; use strict; my %Hash; if(defined $Hash{'ABC'}) { …
Jean
  • 21,665
  • 24
  • 69
  • 119
1
vote
3 answers

Perl Hashref initialize and assign at once

Actually I found a weird behavior when i try to initialize a Perl Hash(ref) and try to assign it via autovivication at once. Here is a short Codesnippet to make it a bit clearer: use Data::Dumper; my $hash->{$_} = 'abc' foreach (1..4); print Dumper…
user1558455
1
vote
4 answers

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…
ennuikiller
  • 46,381
  • 14
  • 112
  • 137
1
vote
2 answers

Perl autovivification with TIEHASH

This is the expected and intuitive behavior of a tied hash to handle $h{a}++: $ perl -E' sub DESTROY {} sub AUTOLOAD { say "$AUTOLOAD @_"; bless {} } tie %h, main; $h{a}++; ' main::TIEHASH main main::FETCH main=HASH(0x7fb6c982a188)…
creaktive
  • 5,193
  • 2
  • 18
  • 32
1
vote
3 answers

Hash created by autovivification has extra keys

This is what I have my %count_words; while (){ my $line= $_; chomp $line; my @words = split (/ /, "$line"); foreach my $word(@words){ $count_words{"$word"}++; } } foreach my $key (%count_words){ print…
Amey
  • 8,470
  • 9
  • 44
  • 63
0
votes
0 answers

Autovivify and attribute like access

How can I make my dict autovivify when I access it like ATTRIBUTE i.e. z.ab class AttrDict(dict): def __init__(self, *args, **kwargs): super(AttrDict, self).__init__(*args, **kwargs) self.__dict__ = self def…
sten
  • 7,028
  • 9
  • 41
  • 63